NSDateformatter not working in IOS 7.0.4 - ios

I was using NSDateformatter for parsing a date. It was working fine til I updated to iOS 7.0.4. But since the update I am getting a nil value,
This is the date I am trying to parse
11/20/2013 3:30:05 PM
Below is the code for the same
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[NSLocale currentLocale]];
NSDate *date = [dateFormatter1 dateFromString:[items objectAtIndex:2]];
But I am getting nil as the date.
How can I make this work in IOS 7.0.4?

Your date is localized, but you are using the device its locale.
But the AM/PM may be different in other locales.
-(void)dateTest {
NSString *inputDate = #"11/20/2013 3:30:05 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [dateFormatter1 dateFromString:inputDate];
NSLog(#"date: %#", date); //OUTPUT date: 2013-11-20 15:30:05 +0000
}

Can you post what string is stored in [items objectAtIndex:2]?
I try it and it works fine for me:
-(void)dateTest
{
NSString *inputDate = #"11/20/2013 3:30:05 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[NSLocale currentLocale]];
NSDate *date = [dateFormatter1 dateFromString:inputDate];
NSLog(#"date: %#", date); //OUTPUT date: 2013-11-20 15:30:05 +0000
}

If it is displaying a different hour you may need to set the default timezone (this happens only after ios7)
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
it seems that the default timezone is the one the device has, so if you no specify the default timezone you might get strange results. Prior to ios7 it was always GMT.

Related

iOS Objective C: Date format returns nil

In my iOS application(Objective C) I am adding date (29th Nov 2016) in database in following format:
2016-11-29 04:08:00 PM
And during displaying it I want to show in following format:
11/29/2016 04:08:00 PM
For this I have added formator like
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *date1 = [dateFormatter dateFromString: StartDateTime];
//here StartDateTime = 2016-11-29 04:08:00 PM
// Convert to new Date Format with /
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *newDate = [dateFormatter stringFromDate:date1];
But here newDate returns nil.
How can I change the date format? Please suggest me.
Thank you.
I have tried your code with below changes:
NSString *StartDateTime = #"2016-11-29 04:08:00 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *date1 = [dateFormatter dateFromString: StartDateTime];
//here StartDateTime = 2016-11-29 04:08:00 PM
// Convert to new Date Format with /
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSString *newDate = [dateFormatter1 stringFromDate:date1];
NSLog(#"%#",newDate);
Here with in your code I have added a new object of NSDateFormatter (For above example dateFormatter1) with new allocation and new date formate.
You will get your answer with this way.
Make sure every time that you have to create new instance of NSDateFormatter every time you want to change the DateTime formate.
And even after trying this option, If you don't get your desired result then please try to run the code in Device, not in Simulator. Because simulator may not return accurate result all the time especially for date and time

How to get the UTC time from NSDate iOS

I want to log the server time in my iOS app , I am using NHNetworkTime library to get the network time, when I try to print the following code its showing GMT, through I have chosen UTC, how to print the date time in UTC?
NSDate* datetime = [NSDate networkDate];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]]; // Prevent adjustment to user's local time zone.
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS Z z "];
NSString* dateTimeInIsoFormatForZuluTimeZone = [dateFormatter stringFromDate:datetime];
NSLog(#"%#",dateTimeInIsoFormatForZuluTimeZone);
this is printing 2016-03-09 06:51:23.406 +0000 GMT
There is no time difference between Coordinated Universal Time (UTC) and Greenwich Mean Time(GMT). you can get more info from here More info
Refer :-
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
[dateFormatter release];
return dateString;
}
Moreover to get time
-(NSString *)getTimeFromDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateFormat = #"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = #"HH:mm:ss";
return [timeFormatter stringFromDate: localDate];
}

NSDateFormatter for day and month name

I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);

Date formatter not working in iOS6, but works in iOS 7

I'm processing a JSON string, which is date time string in the format: 2013-01-07T12:30:00+11:00
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *startString = (NSString*)[dictionary objectForKey:#"start_time"];
NSDate *startTime = [dateFormatter dateFromString:startString];
Works perfectly in iOS7, but startTime is nil in iOS6.0.
Any clue why? What's going on?
Answer is change Z to ZZZZ
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZ"];

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