Convert date from mysql to NSDate - ios

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...

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);

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.

Related

dateFromString results wrong date

Am Trying to convert NSString to NSDate and then store it on Core Data. I added the timezone to NSDateFormatter still it returns wrong output. And is stored in different format in Core Data.
NSString *dateString=#"2015-09-17 01:06:44";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
dateFormatter.locale = [NSLocale currentLocale];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date_f = [dateFormatter dateFromString:dateString];
NSLog(#"date string %#",dateString);
NSLog(#"date %#",date_f);
NSManagedObject *events=[[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:app.context];
[events setValue:date_f forKey:#"start_date"];
Console Output was
date string 2015-09-17 01:06:44
date 2015-09-16 19:36:44 +0000
Data is stored in Core Data as below
start_date = "September 17, 2015";
Thanks.
This looks like a misunderstanding of time zone conversion. A thing that also gave me some headaches in the past :)
Here is what is done:
You have a date string
You tell that this date string is in local time zone
You convert the string to NSDate
NSDate is now an UTC date because there is no time zone information in a NSDate object
Now, about Core Data and NSManagedObject, you can check this answer. I don't know how your are currently getting this start_date value but first thing is to check you Core Data configuration.
Hope this helps.

Date formatting not working for 5/1916

I'm trying to format two NSString to an NSDate with this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/yyyy"];
NSString *dateString = #"5/1916";
NSString *dateString2 = #"6/1916";
NSDate *date = [dateFormatter dateFromString:dateString];
NSDate *date2 = [dateFormatter dateFromString:dateString2];
Guess what:
date is nil and date2 works fine (isn't nil).
Any idea how this come?
Edit:
For those who think it's about the string, the next screenshot has the samen problem.
your code works perfectly fine with following result
Printing description of dateString:
05/1916
Printing description of date:
1916-04-30 18:06:40 +0000
But issue is that NSDate is something which must have day, month , year Just revamp your code to feed dateformatter a correct date.
in most languages, 5/1916 wouldn't be accepted, but 05/1916 is, since the format is MM/yyyy not M/yyyy. the length of string is mandatory.
hope that helps,
eiran

Human Written date from NSString to NSDate

What's the best way to convert something like this (Written to be easily read for humans):
Jun 14, 2013 or another Sep 23, 2009
from an NSString into an NSDate?
I could only find ways to convert if the NSString was written in a more standard format.
you can Convert NSString Date to NSDate like following:
NSString *str1 =#"Jun 14, 2013";
NSString *str2;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd, yyyy"];
NSDate *myDate = [df dateFromString: str1];
str2=[df stringFromDate:myDate];
NSDate *myDate2 = [df dateFromString: str2];
// OUTPUT as your string
NSLog(#"same as your string %#",str2);
// Here you can change date format that you want from actual formatted string.
[df setDateFormat:#"yyyy MMM, dd"];
//Out Put with change Format like 2013 Jun, 14
NSLog(#"diffrent formate %#",[df stringFromDate:myDate2]);
Do not NSLog with NSDate object that show wrong that's why i put NSlog with str2
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.

Problems converting a string date to NSDate

I have this string date:
2014-04-21T07:55:13Z
when I convert that to NSDate I have the hour like 6:55... 1 hours less. WHY?
This is the code I am using to convert:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *newDate = [dateFormatter dateFromString:dateStr];
newDate is now 2014-04-21 06:55:13 +0000 !!!???
what is wrong?
NOTE: That one hour less would make sense if the date was my local time (GMT+1) being converted to GMT. But if that Z is zero offset ( = GMT) the date is already GMT.
I don't think your code is wrong. using this code:-
NSString *dateStr = #"2014-04-21T07:55:13Z";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#" date log %#",date); //2014-04-21 02:25:13 +0000 output
// Convert date object to desired output format
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"string %#",dateStr); //2014-04-21T07:55:13Z output
but NSLog of NSDATE is not output correct according to this NSDate Format outputting wrong date so your code is right.
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: for know that is current or not to actually format the date for pretty human-readable display.
NSLog(#" date is %#",[dateFormat stringFromDate:date]);

Convert NSString back to NSDate

I am using NSDateFormatter to convert the current date to a string (in the format: February 16, 2013). How can I convert this string back to a NSDate object?
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterNoStyle];
The problem appears to be that the month is written out (February vs. 02), and other questions only explain to use NSDateFormatter with a format such as MM-dd-yyyy, which I do not believe is possible here. Must I parse this date manually, convert February to 02, and go from there?
You can use dateFromString of the same NSDateFormatter class to perform backward conversion.
To make it work you need to define dateStyle, so parser will know how text string should be parsed. For the date style that you provided code below will work:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:#"February 16, 2013"];
NSLog(#"%#", date);
If you want to be able to use localized date formats, you should go with templates
NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterLongStyle
timeStyle:NSDateFormatterNoStyle];
NSLog(#"%#", dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:#"MMMdY"
options:0
locale:[NSLocale currentLocale]]];
NSLog(#"%#", [dateFormatter dateFromString:dateString]);
Since you have a fixed format that you wish to parse, you must setup the date formatter with the locale of en_US_POSIX. Then you must set he date format to MMMM dd, yyyy. This will pare any date string that has the full month name, the month day, a comma, then the four-digit year.

Resources