Converting UTC string into NSDate - ios

I have a date-time string returned by the server in UTC format: 2015-04-21T00:54:46.469Z
I am trying to convert this into NSDate. The code is:
NSString *dateString = #"2015-04-21T00:54:46.469Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-DDThh:mm:ss.sZ"];
NSDate *locationDate = [dateFormatter dateFromString:dateString];
The value of locationDate is nil after executing this

You should use
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
or
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
Any way, T must be in quotes. It seems to be a bug in Apple.

Related

Converting a NSString to NSDate

I am trying to convert a NSString into a NSDate as shown below.
The value of NSString *startTime is 2015-06-23T01:37:53Z,
but the value of NSDate *startTimeDate is nil. What is wrong with the code ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
check your date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
change into
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
Swift
check your date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
change into
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
There are at least 2 issues with your date format:
.SSS is used to read milliseconds but variable startTime doesn't contains milliseconds value;
Z represent GMT time zone and must not be escaped in dateFormat string.
Let's try to fix this ussues:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
I recommend to use this NSDateFormatter date formatting table. It's very comprehensive and helpful.
The startTime you've specified doesn't have any milliseconds, so you want to use a dateFormat of:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
If you want to support both styles of XML date strings then I recommend creating two NSDateFormatter instances for both date formats, and try the other if you get nil from the first.
Its simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the NSDateFormatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];

iOS NSDateFormatter returns old date from string

NSDateFormatter dateFromString fetched older date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateformatter dateFromString:#"2015-05-18"];
The above function returns date as 2015-05-17 11:39:18 +0000 instead of
2015-05-18
Actually you are printing NSDate, if you are printing Date then compiler automatically convert your date to string by converting using current timezone. if you want actual converted date in NSString form then add one more line that convert NSDate to NSString like as bellowed.
NSDateFormatter * dFormatter = [[NSDateFormatter alloc] init];
[dFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dFormatter dateFromString:#"2015-05-18"];
NSLog(#"String : %#",[dFormatter stringFromDate:date]);
Output :
2015-05-18
this is working code to show the current date...might be solve your problem..you should go for this...
NSDate *Date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateFormated = [dateFormatter stringFromDate:Date];
NSLog(#"%#", dateFormated);

NSDateFormatter issue when date string convert into NSDate object

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE,d MMM YYYY"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:#"Sun,2 Nov 2014"];
output date from string giving wrong output like 2013-12-21 18:30:00 +0000
Let me guess, you are doing this to test the output:
NSLog(#"%#", dateFromString);
instead do:
NSLog(#"%#", [dateFormatter stringFromDate:dateFromString]);
This is because NSLog() will call [NSDate description] to format the date and that won't account for any formatting or time zone you may want.

Conversion of NSString to NSDate failing

I have the following piece of code..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
I am getting the NSLog statement as Parsed Date.. 2011-12-25 00:00:00 +0000. I would appreciate any help in resolving this. I just want to convert that NSString to its NSDate equivalent. Tried using NSTimeZone as well, but no effect. What am I missing here?
You need to use lower case year indication, yyyy instead of YYYY:
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
If you want to get the timezone right you need to set the timezone of the NSDateFormatter. By default it uses the timezone of your device. For the time at GMT use the following.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
You are using an incorrect format string. See the documentation. Basically, capital YYYY is "week of year year". Try to use lowercase yyyy instead, i.e.
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];

Troubles with parsing ISO8601 time in iOS

I'm trying to convert this string "2011-11-23T17:59:00Z" to an NSDate.
I've seen many people have this problem, but everyone has a slightly different format. I haven't been able to hack a solution.
I've tried code such as:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.timeStyle = NSDateFormatterFullStyle;
[dateFormat setDateFormat: #"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSString* date = ""2011-11-23T17:59:00Z"";
NSString *dateString = [dateFormat stringFromDate: date];
dateString always comes back NULL
NSDateFormatter stringFromDate takes an NSDate object and you're passing it a string.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Try NSDateFormatter dateFromString.
If you use dateFromString:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: #"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString* dateStr = #"2011-11-23T17:59:00Z";
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"date: %#", date);
Outputs:
2011-12-14 00:34:57.587 Craplet[440:707] date: 2011-11-23 22:59:00 +0000

Resources