1

I have a varchar in mysql that holds dates. I have been trying to convert it into an NSDate, but nothing I have tried yet works. The NSString from mysql looks like this:

August 11, 2012, 10:17 AM

All the posts so far relating to NSString to NSDate conversions have not yet worked for this string. If someone could please help...

Daniel
  • 23,129
  • 12
  • 109
  • 154

2 Answers2

2

I assume you use JSON for getting data from MySQL(server) and have a JSON object called myJSONObject

NOTE: myJSONObject must be serialized using some framework like NSJSONSerialization

EDIT with some detailed question links

You have asked to convert data to August 11, 2012, 10:17 AM but my sample code tries to convert as 2012-08-11 10:17:.., i edited my sample code for you. For more, take a look at NSDateFormatter

ATTENTION PLEASE: you can get the variable month_from_date using NSDateComponents. There are some questions about it here and here.. If you can't, please open a new post.

 NSString* dateString = [myJSONObject objectForKey:@"date"];

            NSDateFormatter* fmt = [NSDateFormatter new];
            [fmt setTimeZone:[NSTimeZone defaultTimeZone]]; 
           // [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
           [fmt setDateFormat:@"%@ DD,YYYY, HH:mm",month_from_date]; 
           NSDate* dateFromString = [fmt dateFromString:dateString];
NSLog("Here is date from string: %@",dateFromString);
Community
  • 1
  • 1
ilhnctn
  • 2,210
  • 3
  • 23
  • 41
  • Thanks for the reply. I parse the json similar to the way you posted. I put in the code and still get null however. It could be that the dateformat is wrong- Again, Friday, August 3, 2012, 11:20 AM - is what needs to be converted. – Daniel Shaffer Aug 09 '12 at 21:52
2

to convert a NSString into a NSDate, please, use the NSDateFormatter.

NSString *_dateString = @"August 11, 2012, 10:17 AM";
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setDateFormat:@"MMMM dd, yyyy, HH:mm a"];
NSDate *_date = [_dateFormatter dateFromString:_dateString];
NSLog(@"raw date : %@", _date);
NSLog(@"formatted date : %@", [_dateFormatter stringFromDate:_date]);

the output NSDate will contains the date:

raw date : 2012-08-10 23:17:00 +0000
formatted date : August 11, 2012, 11:17 AM

you can format the NSDate as you'd like for a different output, it is up to up now, but you can work with the NSDate object which contains the date.

NOTE: we don't know the timezone from the input date when we are converting it, so the NSDate using the defaults for it. if you set the current timezone you will get the correct date after conversion.

holex
  • 23,961
  • 7
  • 62
  • 76
  • but he wants to get August not 08 for example – ilhnctn Aug 10 '12 at 08:07
  • he wrote the _input_ string is `August 11, 2012, 10:17 AM` and he likes to convert into `NSDate` as _output_. did I misunderstand something? – holex Aug 10 '12 at 08:10
  • your output will give 08, 2012 but he wants August, 2012. Not an important point but there must be some enumerations for this. my answer explains a little – ilhnctn Aug 10 '12 at 08:56
  • @ilis, I understand what my output is. I've said I still guess he wants to convert the date **from** that format, not **to** that format. however, for the same output he could use the same `NSDateFormatter` as in my answer with the `-stringFromDate:` method, if he really wants that output, what you assume. I'll update to my answer for it. – holex Aug 10 '12 at 09:15
  • @@holex, this time when i read the question i really couldnt understand what he wants. He says "a varchar in mysql that holds dates. I have been trying to convert it into an NSDate. The NSString from mysql looks like this: August 11, 2012, 10:17 AM" but in the second paragparh i think he wants NSString from NSDate.. I'm not sure from now on – ilhnctn Aug 10 '12 at 09:21
  • @ilis, on my screen in the last section he mentions: `NSString` _to_ `NSDate` _conversions_, this is why I thinks he wants conversion **from** that input `NSString` to `NSDate`, but we will see what he says. :) – holex Aug 10 '12 at 09:26
  • @ilis I don't know the truth now. :) I've just read the title of the thread, it says _Convert date **from** mysql **to** `NSDate`_ but I'm confused now. – holex Aug 10 '12 at 09:48
  • you are welcome, @DanielShaffer! :) please, don't forget to upvote and accept the answer, if you think it was helpful. – holex Aug 10 '12 at 15:36
  • hey guys, what was the thing exactly asked here, i haven't get it yet:)) – ilhnctn Aug 15 '12 at 08:13
  • @ilis, what was your question then? you've asked a `NSString` to `NSDate` conversion... – holex Aug 15 '12 at 08:44
  • but from the question it is not clear if he asks for NSString to NSDate or vice versa – ilhnctn Aug 15 '12 at 08:59
  • @ilis, oh sorry! :( for a second I though you are the asker. :) yes, perhaps he is still enjoying the solution and he forgot us. :D in most of the cases it happens. – holex Aug 15 '12 at 09:10
  • :) i see. i'm aware of the solution but really this question made me go crazy(like Sheldon Cooper when can't understand a complicated mathematic formula or a series of Star Wars:) – ilhnctn Aug 15 '12 at 09:19