Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-03-2009, 10:25 AM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 104
dobry is on a distinguished road
Default how to get NSDate from String "2009-03-02 00:00:00 +0200"

A NSDate is in SQLite saved as a String like "2009-03-02 00:00:00 +0200".
How can I get back the NSDate if the date is in a NSString with this format?

For Firmware 2.2.1 it works like this:
Code:
- (NSDate *)dateFromString:(NSString *)parameterString {
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_0];
  [dateFormatter setDateFormat:@"%m/%d/%Y %H:%M:%S %z"];
  return [[dateFormatter dateFromString:parameterString] copy];
}
But in FW 3.0 NSDateFormatterBehavior10_0 is not available anymore. What is the solution for FW 3.0?
dobry is offline   Reply With Quote
Old 08-03-2009, 02:22 PM   #2 (permalink)
Registered Member
 
MikesTooLz's Avatar
 
Join Date: Jan 2009
Location: Miami, Florida
Posts: 211
MikesTooLz is on a distinguished road
Send a message via AIM to MikesTooLz
Default

I would like to know as well.
I have an app that loads RSS feeds and the published dates are formated like that. In my case I just needed to check if the date is for the current year. I ended up just checking the string to see if it contains 2009.
MikesTooLz is offline   Reply With Quote
Old 08-07-2009, 04:17 AM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 104
dobry is on a distinguished road
Default

nobody has a solution?
dobry is offline   Reply With Quote
Old 08-07-2009, 05:28 AM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: Chennai
Age: 25
Posts: 124
nagarajan is on a distinguished road
Default

Quote:
Originally Posted by dobry View Post
A NSDate is in SQLite saved as a String like "2009-03-02 00:00:00 +0200".
How can I get back the NSDate if the date is in a NSString with this format?

For Firmware 2.2.1 it works like this:
Code:
- (NSDate *)dateFromString:(NSString *)parameterString {
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_0];
  [dateFormatter setDateFormat:@"%m/%d/%Y %H:%M:%S %z"];
  return [[dateFormatter dateFromString:parameterString] copy];
}
But in FW 3.0 NSDateFormatterBehavior10_0 is not available anymore. What is the solution for FW 3.0?
A simple following conversion can do this!!!

- (NSDate *)dateFromStringNSString *)string.

Check it up.

Regards,
Nagarajan T.
nagarajan is offline   Reply With Quote
Old 08-07-2009, 05:32 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: Chennai
Age: 25
Posts: 124
nagarajan is on a distinguished road
Default

Quote:
Originally Posted by MikesTooLz View Post
I would like to know as well.
I have an app that loads RSS feeds and the published dates are formated like that. In my case I just needed to check if the date is for the current year. I ended up just checking the string to see if it contains 2009.
Could you please explain how to check whether a string is available in a set of text???

Regards,
Nagarajan T.
nagarajan is offline   Reply With Quote
Old 08-07-2009, 07:07 AM   #6 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 104
dobry is on a distinguished road
Default

@nagarajan: It doesn't work with FW 3.0:
Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLog(@"datum=%@",[dateFormatter dateFromString:@"2009-03-02 00:00:00 +0200"]);
NSLog shows: datum=1970-01-01 03:00:00 +0300
dobry is offline   Reply With Quote
Old 08-08-2009, 12:15 AM   #7 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: Chennai
Age: 25
Posts: 124
nagarajan is on a distinguished road
Default

Then you can store and retrieve it as a Date variable in your database.

Regards,
Nagarajan T.
nagarajan is offline   Reply With Quote
Old 08-08-2009, 01:10 AM   #8 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: Denver
Posts: 172
BSDimwit is on a distinguished road
Default Use a double instead

Setup your table to store the date as a REAL type, then use the following to store and retrieve the date.

Store the date
Code:
sqlite3_bind_double(dehydrate_statement, 4, [myDate timeIntervalSince1970]);
Retrieve the date
Code:
self.myDate = [NSDate dateWithTimeIntervalSince1970:sqlite3_column_double(init_statement, 2)]
Or if you must do it your way...
Code:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
		
NSString *dateString = [dateFormatter stringFromDate:date];

NSDate *myDate = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Works like a champ in my code.

Last edited by BSDimwit; 08-08-2009 at 01:31 AM.
BSDimwit is offline   Reply With Quote
Old 08-08-2009, 05:59 AM   #9 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 104
dobry is on a distinguished road
Default

Thank you very much BSDimwit.
dobry is offline   Reply With Quote
Old 11-30-2009, 04:43 AM   #10 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 1
chesterbr is on a distinguished road
Default Deprecated

Quote:
Originally Posted by nagarajan View Post
A simple following conversion can do this!!!

- (NSDate *)dateFromStringNSString *)string.

Check it up.

Regards,
Nagarajan T.
Be careful: I used that on an app, and got a warning. Ignored it just to find out it is not documented and Apple rejected my app for that :-(
chesterbr is offline   Reply With Quote
Old 11-30-2009, 11:13 AM   #11 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: Denver
Posts: 172
BSDimwit is on a distinguished road
Default

Quote:
Originally Posted by chesterbr View Post
Be careful: I used that on an app, and got a warning. Ignored it just to find out it is not documented and Apple rejected my app for that :-(
I believe you are mistaken. That method is not undocumented. Go check the NSDateFormatter class yourself.

Quote:
dateFromString:
Returns a date representation of a given string interpreted using the receiver’s current settings.

- (NSDate *)dateFromStringNSString *)string

Parameters
string
The string to parse.

Return Value
A date representation of string interpreted using the receiver’s current settings.

Availability
Available in iPhone OS 2.0 and later.
See Also
– getObjectValue:forString:range:error:
– stringFromDate:
Related Sample Code
SeismicXML
URLCache
Declared In
NSDateFormatter.h
BSDimwit is offline   Reply With Quote
Old 12-01-2009, 04:54 AM   #12 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: Chennai
Age: 25
Posts: 124
nagarajan is on a distinguished road
Default

Yes, what BSDimwit said was correct.
Go and search for dateFromString, you will find a lot of conversations here.
nagarajan is offline   Reply With Quote
Old 09-13-2011, 01:36 AM   #13 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 10
Yuvaraj.M is on a distinguished road
Question NSDate Problem?

Hi friends.

Me too have the same problem. I have used the code that is given by Mr.BSDimwit. This is converting the the date from NSString to NSDate. But, there the time is changed. Here i have show the code that is i tried,

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];

NSString *dateString = [dateFormatter stringFromDate:[timePicker date]];
NSLog(@"DateString : %@", dateString);

NSDate *myDate = [dateFormatter dateFromString:dateString];
NSLog(@"My Date : %@", myDate);
[dateFormatter release];

My NSLog output is:
DateString : 2011-09-13 11:51:11 +0530 (This is correct date and time from 'stringFromDate')
My Date : 2011-09-13 06:21:11 +0000(After converting NSString from NSDate it shows the wrong time).

I want the result in NSDate format with time, how to omit +0000 digits from the date? Thanks for reading my poor english.

How to solve this problem? Can any one help me please?
Thanks for spending your valuable time with me.
Thanks in advance.
Yuvaraj.M is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 321
11 members and 310 guests
chiataytuday, coolman, givensur, guusleijsten, ipodphone, jbro, mediaspree, mottdog, mtl_tech_guy, Punkjumper, vilisei
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:21 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0