nil returned from dateFromString NSDateFormatter - ios

I am trying to create a NSDate from some strings of dates I have.
Here is what I am doing.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate *now = [NSDate date];
NSDate *deliveryETA = [dateFormatter dateFromString:deliveryDate];
NSDate *createDate = [dateFormatter dateFromString:shipDate];
deliveryETA is always nil with this string 2014-12-23T23:59:59 but createDate is always properly initialized.
Any idea why this is?

Change NSDateFormatter for deliveryETA Like
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
// chanegn this line
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *now = [NSDate date];
NSDate *deliveryETA = [dateFormatter dateFromString:deliveryDate];
// add this line
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate *createDate = [dateFormatter dateFromString:shipDate];
tell me what happen...

NSDate *deliveryETA = [dateFormatter dateFromString:deliveryDate];
for deliveryETA to have the value the input string MUST contain the format yyyy-MM-dd'T'HH:mm:ssZZZZZ

Related

NSDate current date returning nil

In some scenario, I am getting currentDateString as nil.My app is in live so i can't find out exact scenario.
NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
I tried this code. I got correct results. Please set DateFormatter Locale
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];

iOs - error with NSDateFormatter

I have a Date with value 2015-05-31 22:00:00 UTC (I parse it from a NSString with value "01-06-2015" and it give that value) and when I parse it it become 01-06 why??
I need to get on nextdate the value = 2015-06-1 22:00:00 UTC because I need to get the day of the week
Code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"es_ES"];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:enUSPOSIXLocale];
NSDate *nextDate = [theCalendar dateByAddingComponents:comps2 toDate:date2 options:0];
NSString* primeraFecha = [dateFormatter stringFromDate:nextDate];
Values at runtime
Try with this code,
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[[NSTimeZone alloc] initWithName:#"Europe/London"]];
return [dateFormatter dateFromString:dateString];
You should set the time zone of your formatter:
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

Which dateFormat should I choose while converting NSString to NSDate?

I have a NSString like #"2014-11-27T10:54:08.185Z"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate * date = [dateFormatter dateFromString:self.creationTime];
I tried a lot of different formatter strings, but the date is always nil.
Or is there another problem here?
The hour in your date string format seems to be in the 24h format.
So you need to use HH instead of hh in your dateFormat.
More info
Try
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.creationTime];
self.dateLabel.text = [formatter stringFromDate:date];
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];
Please try this...:)

Getting NSDate from NSString using NSDateFormatter

In my projet I am getting date value in the below format,
NSString* str = #"04-10-2011 20:00:00";
Then I am trying to convert it to date
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM-dd-yyyy HH:mm:ss"];
NSDate *date = [df dateFromString:str];
It is getting nil. Am I giving wrong date format? Can any one let me know?
Your code seems pretty, try to use timezone property of dateformatter to get the exact date
NSString* str = #"04-10-2011 20:00:00";
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:locale];
[df setDateFormat:#"MM-dd-yyyy HH:mm:ss"];
NSDate *date = [df dateFromString:str];
NSLog(#"Date is %#", date);
[df setDateFormat:#"mm-dd-yyyy HH:mm:ss"];

Date Format - time zone

I receive from the server a string with a date that looks like this: "2013-03-29T15:27:00Z". How can I format it and transform it into an NSDate?
Try this:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [formatter dateFromString:#"2013-03-29T15:27:00Z"];
NSString *departTimeDate = #"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [dateFormatter dateFromString:departTimeDate];
NSLog(#"Expected Result___ %#",date);
NSString *string=#"2013-03-29T15:27:00Z";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date=[dateFormatter dateFromString:string];

Resources