Covert string with timezone to Date - ios

Here is the date string
NSString * dateString = #"Mon Sep 29 14:40:00 2014 PET";
How to convert it into NSDate?
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setCalendar:[NSCalendar currentCalendar]];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zzz"];
NSDate* date = [dateFormatter dateFromString:dateString];
Above code doesn't work.
what is the correct date format for above case?

the problem is your timezone definition (PET), because that is not on list of the recognisable timezones (please don't ask why not); this timezone is mostly known as UTC-5 in Apple's system, therefore:
NSString *_dateString = #"Mon Sep 29 14:40:00 2014 PET";
_dateString = [_dateString stringByReplacingOccurrencesOfString:#"PET" withString:#"UTC-5"];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[_dateFormatter setCalendar:[NSCalendar currentCalendar]];
[_dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy zzz"];
NSDate *_date = [_dateFormatter dateFromString:_dateString];
NSLog(#"date : %#", _date);
and the console says:
date : 2014-09-29 19:40:00 +0000
which is the correct representation of the original date in GMT+0 timezone.

Related

NSDateFormatter returns nil in a simply date format change

I'm trying to convert this date:
Jun 23, 2015 7:53:04 PM
coming from a server response, to a different format using NSDateFormatter but it is constantly returning nil and (null) values in the console if I try to NSLog it. The code I'm using to do so is the following:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSDate *date = [dateFormatter dateFromString:[contentData valueForKey:#"startDate"]];
Isn't MMM dd, yyyy hh:mm:ss a the correct format to parse the date?
UPDATE
I changed the code in the following way:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSDate *date = [dateFormatter dateFromString:#"Jun 23, 2015 7:53:04 PM"];
But it still doesn't work
This is working for me.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSDate *date = [dateFormatter dateFromString:#"Jun 23, 2015 7:53:04 PM"];
NSLog(#"date: %#",date);//date: 2015-06-23 13:53:04 +0000
NSString* formattedDate = [dateFormatter stringFromDate:date];
NSLog(#"Formatted Date: %#",formattedDate);//Formatted Date: Jun 23, 2015 07:53:04 PM

iOS create date from string

How can I make this format in iOS: "Mon Jan 26 07:57:33 +0000 2015"?
I have tried this
NSString *createdAt=#"Mon Jan 26 07:57:33 +0000 2015";
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"ddd MMM dd HH:mm:ss zzzz yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter setTimeZone:gmt];
NSDate *date=[formatter dateFromString:createdAt];
but this returns me nil always
// Convert string to date
//Fri, 23 Jan 2015 14:24:24 IST
NSString *beginString = [currentFields objectForKey:#"pubDate"];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Kiev"]];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
dateFromString = [dateFormat dateFromString:beginString];
This is my example. Your date formatter should be the same as your original string date.

NSDateFormatter not giving expected results

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMM YYYY"];
NSLog(#"formatted date %#",[formatter dateFromString:#"14 Oct 2013"]);
I am trying to format the date 14 Oct 2013, to get an NSDate object out of it. But for some reason, it is not formatted as expected.
Here's the date object that I get after logging it on console,
formatted date 2012-12-22 18:30:00 +0000
Anything wrong I am doing here?
You should use lowercase yyyy because uppercase one represent a week based calendar year and you should specify timezone:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMM yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[formatter setTimeZone:gmt];
NSLog(#"formatted date %#",[formatter dateFromString:#"14 Oct 2013"]);

IST time zone not getting parsed

I have a string Tue Feb 23 10:12:48 IST 2014, I want to get the NSdate object form this. I am using below code snippet
NSString * buildTime = #"Tue Feb 23 10:12:48 IST 2014";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSDate *date = [formatter dateFromString:buildTime ];
Every time I am getting nil value to date variable. If I am changing IST to GMT in build time then NSDate is perfectly coming. Can any one correct me where I am doing mistake in this.
Use small 'zzz' instead of 'ZZZ',
NSString * buildTime = #"Tue Feb 23 10:12:48 IST 2014";
NSLocale *indianEnglishLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:indianEnglishLocale];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSDate *date = [dateFormatter dateFromString:buildTime];
NSLog(#"date %#",date);
NSLog(#"string %#",[dateFormatter stringFromDate:date]);

NSDate in ios conversion

i wanto convert the string 2013-03-19 13:41:57 +0000 to this format Wed Feb 27 2013 00:00:00 GMT+0530 (India Standard Time) in iOS .please help me any help will be highly appreciated.
i tried some thing like this:
NSString * clippedString = #"2013-03-19 13:41:57 +0000 ";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE MMM d yyyy HH:mm:ss Z"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:clippedString];
I accepted the challenge :)
NSString *string=#"2013-03-19 13:41:57 +0000";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
NSDate *date=[dateFormatter dateFromString:string];
[dateFormatter setDateFormat:#"EEE MMM dd yyyy HH:mm:ss zzz zzzz"];
NSLog(#"%#",[dateFormatter stringFromDate:date]);

Resources