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];
Related
Tried converting NSString with the date 'Saturday 22 August 2015 17:30' to NSDate but it comes out with '2014-12-27 17:30:00 UTC'. I don't even care that the format comes out wrong, i just want it to convert it to NSDate correctly.
currentFixtures.timeDate is a NSString containing 'Saturday 22 August 2015 17:30'
fixtureDate is an NSDate that comes back with '2014-12-27 17:30:00 UTC'
NSDateFormatter *dateFormatFixture = [[NSDateFormatter alloc]init];
[dateFormatFixture setDateFormat:#"EEEE d MMMM YYYY HH:mm"];
[dateFormatFixture setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en-GB"]];
[dateFormatFixture setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *fixtureDate = [[NSDate alloc] init];
fixtureDate = [dateFormatFixture dateFromString:currentFixture.timeDate];
EEEE d MMMM yyyy H:m works, thanks to maddy
I am trying to create a date out of my date string that goes:
Tue, 29 Jul 2014 17:22:30 +0200
I am using this code but it still gives me null as I try to dateFromString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd LLL YYYY hh:mm:ss +0200"];
NSLog(#"The date: %#", [dateFormatter dateFromString:bazosItem.itemDate]);
so the output is:
The date: (null)
I have been searching for the right dateFormat in the internet and have rechecked it multiple times and I have no idea what is going wrong with the date.
There are some mistakes in your format, first the YYYY should be yyyy second the hh is for AM/PM hour not 24 hours for this you should use HH.
Third and most important you should the the date format in which language the date is by adding a locale:
NSString *temp = #"Tue, 29 Jul 2014 17:22:30 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"EEE, dd LLL yyyy HH:mm:ss +0200"];
NSLog(#"The date: %#", [dateFormatter dateFromString:temp]);
Your date format seems to be incorrect. Try the following
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss +0200"];
I always have trouble with this, and keep seem to find a good resource to learn this exhaustively. I am trying to use the date formatter in Xcode objective C, but I am not setting the date format correctly. Here is my data:
Fri, 27 Jun 2014 12:33:18 +0000
Can someone assist me or point me in the right direction? Trying the below code, but it is not working.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"EEE, d LLL yyyy HH:mm:ss Z"];
Update: the format below might help, as it shows some zero padding on the day.
Wed, 02 Jul 2014 11:47:35 +0000
According to the Unicode Technical Standard #35, "LLL" is the date format for a "stand-alone month" which is "a month name without an associated day number".
You should use "MMM" instead:
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss Z"];
Example:
NSString *str = #"Fri, 27 Jun 2014 12:33:18 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:str];
NSLog(#"%#", date);
// Output: 2014-06-27 12:33:18 +0000
I have an NSString that is 05/08/2014. I want to convert that to an NSDate. However, I also need to add in time, so that the resulting NSDate looks like this:
Thu, 8 May 2014 00:00:00 -0500
The time is not important, I just need it to show midnight at the designated timezone.
I have tried:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:textdate.text];
[dateFormatter release];
NSLog(#"%#", dateFromString);
But the date comes back as (null).
Your date format is wrong for the first conversion. What you need is first to convert from string to date from one format and form the new date into the new string format. Something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"]; //convert the string into date (american time zone)
NSDate *theDate = [formatter dateFromString:textdate.text];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];// as #Logan suggested
NSString *newDate = [formatter stringFromDate:theDate];
EEE, d MMM yyyy HH:mm:ss ZZZ
Your time zone code was lowercase instead of uppercase.
zzz corresponds to PDT
ZZZ corresponds to -0500
UTS 35
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]);