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]);
Related
I created a NSDateFormatter but sometimes it creates an NSDate out of a given string sometimes it doesn't on the same device (Simulator).
This is my code:
NSString *lastModifiedString = [metaData objectForKey:#"Last-Modified"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss Z"];
NSDate *onlineModDate = [dateFormatter dateFromString:lastModifiedString];
For Example for the string
"Fri, 09 Jan 2015 15:08:01 GMT" it returns nil but for "Fri, 12 Dec 2014 09:15:52 GMT" it returns the right NSDate.
I tried to add parenthesis to the dateformat and I tried to set locale to "en_US_POSIX" and i tried a little z instead of a big one but neither of it worked.
What am I doing wrong? Thanks for your help.
NSDateFormatter follows the Unicode standard for date and time patterns. Use 'H' for the hour on a 24-hour clock:
So in your code, the correct way should be:
NSString *lastModifiedString = [metaData objectForKey:#"Last-Modified"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *onlineModDate = [dateFormatter dateFromString:lastModifiedString];
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.
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.
I'm trying to convert a string to NSDate. And do not understand what is wrong with my code.
dateString = [NSString stringWithFormat:#"Sun 02 feb 14 12:30"];
NSLog(#"dateString %#",dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setDateFormat:#"EEE dd MMM yy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"dateFromString %#",dateFromString);
I get nil. I tried with different dates (same format), always nil.
eg. Thu 07 nov 13 12:45
Does anybody see the mistake?
You are missing the Locale for the date, set the locale of the date formatter to english to make sure it know the date is in english:
NSString *dateString = #"Sun 02 feb 14 12:30";
NSLog(#"dateString %#",dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE dd MMM yy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en"] autorelease]];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"dateFromString %#",dateFromString);
Also there is no need to alloc the date here NSDate *dateFromString = [[NSDate alloc] init];. If you are not working with ARC you just leaked a NSDate object. If you are using ARC remove the autorelease from the NSLocale.
dateString = [NSString stringWithFormat:#"Sun 02 feb 14 12:30"];
NSLog(#"dateString %#",dateString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setDateFormat:#"EEE dd MMM yy HH:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *dateFromString = [NSDate alloc];
dateFromString = [dateFormatter dateFromString:dateString];
This code gives as output: dateFromString 2014-02-02 12:30:00 +0000 which is correct.
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]);