NSDate conversion in ios - iphone

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);

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);

Date will be different when convert from NSString to NSDate

This is My code
NSString * dateStr = #"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"dateStr : %# , date : %#",dateStr,date);
But my Date will be one Day behind the actual date. See this log
dateStr : 7/25/2013 12:00:00 AM , date : 2013-07-24 18:30:00 +0000
What am I doing wrong?
Try this.
NSString * dateStr = #"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"dateStr : %# , date : %#",dateStr,date);
Try Something like this . this will help you
NSString * dateStr = #"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSString * dateStr = #"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"dateStr : %# , date : %#",dateStr,date);
Setting the time zone fixes this issue

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.

NSDateFormatter issue while converting NSDate to NSString

NSDateFormatter is providing a default date of
2001-01-01 00:00:00 +0000
I need it to format in the format of January, 01 2013 04:00:00
Here is my code:
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM, dd yyyy HH:mm:ss"];
NSDate *startDt=[_calendarDetailsDict objectForKey:#"dtStart"];
NSDate *endDt=[_calendarDetailsDict objectForKey:#"dtEnd"];
NSString *str=[formatter stringFromDate:startDt];
NSString *str1=[formatter stringFromDate:endDt];
NSString *dateStr =#"2012-09-15 00:11:59";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
dateFormat setDateFormat:#"MMMM, dd yyyy HH:mm:ss"];
NSString *dateString = [dateFormat stringFromDate:date];
NSLog(#"date: %#", dateString);
may help u.

iPhone SDK NSString To NSDate

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"];