Unable to convert String to date - iphone

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

Related

DateFormat Conversion

I have a string that is in this format:
2012-11-08T13:50:05.284-08:00
I need to convert it into this format:
November 8, 2012 1:50 PM,
I have identified the pattern of the date to YYYY-MM-DDThh:mm:ss.sTZD, like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DDThh:mm:ss.sTZD"];
NSDate *date = [dateFormat dateFromString:#"2012-11-08T13:50:05.284-08:00"];
[dateFormat setDateFormat:#"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];
Could anyone please tell me what's wrong here? Is the format template correct or not?
Try this:As I getting valid output.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.S'-'S':'S"];
NSDate *date = [dateFormat dateFromString:#"2012-11-08T13:50:05.250-8:00"];
[dateFormat setDateFormat:#"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];
NSLog(#"newDateStr is %#", newDateStr);
You can try like this just ( just remove last 5 characters from the string )
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [dateFormat dateFromString:#"2012-11-08T13:50:05.284"];
[dateFormat setDateFormat:#"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];

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

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.

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.