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.
Related
I have NSDate with value 2015-12-27 +0000 and when I convert this NSDate to NSString with format MMM, YYYY I am getting:
NSString as Dec, 2016
This is because the capital Y in your date format specifies the year in the ISO week date system, not the Gregorian calendar.
I imagine your code may look something like:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM, YYYY"];
NSLog(#"Date: %#", [df stringFromDate:yourDate]);
Instead try using the format:
[df setDateFormat:#"MMM, yyyy"];
When using format strings such as this to specify your output format, NSDateFormatter uses the conventions from Unicode Technical Standard #35 (which describes the difference between Y and y if you want more detail).
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]);
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.
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.
I have data in NSString, I need to display it as Oct 3, 2011. I am having trouble in converting nsstring into NSDate and then again display it as NSString.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy "];
NSDate *dateFromString = [dateFormatter dateFromString:myString];
myString is 2011-10-3 00:00:00
First set the date formatter time zone. If the string you're receiving is GMT/UTC, set the timezone to that.
Next set the date format to match the incoming date pattern. Do dateFromString.
Then set the date format to match the desired output format. Also set the output timezone, if different. Do stringFromDate, using as input the NSDate object from the previous operation.