iPhone SDK NSString To NSDate - iphone

I got a string from parsing a XML file which looks like this: Fri, 09 Apr 2010 00:00:45 +0200
and the corresponding pattern should be this "EEE, dd MMM yyyy HH:mm:ss ZZ", but I get (null).
This is my code:
NSString *dateString = #"Fri, 09 Apr 2010 00:00:45 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"date:%#",date); // result date:(null)
Edit:
This works for me now, I had to switch to en-US locale:
NSLocale* usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSDate *date = [dateFormatter dateFromString:dateString];

Your code worked for me when I changed this line:
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZ"];
to this line:
[dateFormatter setDateFormat:#"EEE',' dd MMM yyyy HH:mm:ss ZZ"];

Related

Unable to convert String to date

This is my String "Oct 21 2013 12:55:22:000PM" And i am not able to convert it in the Date.
This is the code i have tried.
NSString * appLatUpdateStr = #"Oct 21 2013 12:55:22:000PM"
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormat setDateFormat:#"LLLL dd YYYY hh:mm:ss:SSSa"];
NSDate *dbLastUpdateDate = [dateFormat dateFromString:appLatUpdateStr];
NSLog(#"userUpdateResponse : %#",dbLastUpdateDate);
Its just giving me null value..
For month you need MMM not LLLL
NSString * appLatUpdateStr = #"Oct 21 2013 12:55:22:000PM"
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormat setDateFormat:#"MMM dd yyyy hh:mm:ss:SSSa"];
NSDate *dbLastUpdateDate = [dateFormat dateFromString:appLatUpdateStr];
NSLog(#"userUpdateResponse : %#",dbLastUpdateDate);

NSDate conversion in ios

I am getting a NSString Sun Feb 24 2013 00:00:00 GMT+0530 (India Standard Time) I want to
convert that into dd-mm-yy in ios . This is what i tried am not getting the output as I
wanted. Convert string to date object:
NSString *dateStr = #"Sun Feb 24 2013 00:00:00 GMT+0530 (India Standard Time) " ;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString: stripped];
Thanks in advance.
NSDateFormatter *sdateFormatter = [[NSDateFormatter alloc] init];
sdateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[sdateFormatter setDateFormat:#"E MMM d yyyy HH:mm:ss 'GMT'zzz '(IST)'"];
NSDate *sdate = [sdateFormatter dateFromString:stringDate];
#"Sun Feb 24 2013 00:00:00 GMT+0530 (India Standard Time) "
Remove
(India Standard Time)
then do the formattings
NSString *dateStr = #"Sun Feb 24 2013 00:00:00 GMT+0530" ;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE LLLL dd yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSLog(#"%#",[dateFormat stringFromDate:date]);
First remove the "GMT+0530 (India Standard Time)" text,
NSString *dateStr = #"Sun Feb 24 2013 00:00:00" ;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM d yyyy HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
Try this : It worked for me
NSString *dateStr = #"Sun Feb 24 2013 00:00:00 GMT+0530";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE MMM d yyyy HH:mm:ss Z"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:dateStr];
Try this
NSString *dateStr = #"Sun Feb 24 2013 00:00:00 GMT+0530 (India Standard Time) " ;
NSString *clippedString = [dateStr substringToIndex:dateStr.length - 23];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EE LLLL dd yyyy HH:mm:ss Z"];
NSDate *date = [formatter dateFromString:clippedString];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSString *newDateString = [formatter stringFromDate:date];
NSString *dateStr = #"Sun Feb 24 2013 00:00:00 GMT+0530" ;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC+0530"]];
[dateFormatter setDateFormat:#"EE LLL dd yyyy HH:mm:ss zzz"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"date : %#",newDateString);

convert date string to NSDate in objective c/iOS

I have this string ----> Sun, 16 Dec 2012 15:30:22 +0000
I want to convert it to NSDate. I have tried the below code but it is giving me null
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *date = [[NSDate alloc] init];
date = [df dateFromString: #"Sun, 16 Dec 2012 15:30:22 +0000"];
NSLog(#"date: %#", date);
I am not getting the issue ?
Any guidance plz
The dateFormat of the formatter needs to match the format of the string you are providing it with.
Therefore something like this should work
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"EEE, dd MMM yyyy HH:mm:ss ZZZ";
NSLog(#"%#", [formatter dateFromString:#"Fri, 18 Jan 2013 15:30:22 +0000"]);
To look up what the specifiers actually mean check Unicode Technical Standard #35
Try this code
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [df dateFromString: #"Sun, 16 Dec 2012"];
NSLog(#"date: %#", date);
Hope it helps you..
EDIT :-
For your string
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMM yyyy HH:mm:ss z"];
NSDate *date = [[NSDate alloc] init];
date = [df dateFromString: #"Sun, 16 Dec 2012 15:30:22 +0000"];
NSLog(#"date: %#", date);
Try this to get NSDate from String
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
It works fine for me.

How to convert NSString in to NSDate format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
How to convert this NSString(Tue, 13 Mar 2012 00:00:00 -0500) into NSDate format
NSDateFormatter uses the Unicode Date Format Patterns.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormatter dateFromString:stringDate ];
[dateFormatter release];
Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init] ;
dateFromString = [dateFormatter dateFromString:str];
[dateFormatter release];

dateformatter works in simulator but not on device

this code works on simulator ... but not on my ipod touch... on ipod I see the unformated date :-(
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] retain];
//Date Before = Sun, 20 Jun 2010 06:00:00 +020
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *d = [formatter dateFromString:string];
[formatter setDateFormat:#"dd.MM.yyyy HH:mm"];
string = [NSString stringWithString:[formatter stringFromDate:d]];
[currentDate appendString:string];
On the device, you need to explicitly set your locale.
Insert this line:
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
Right after:
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];