convert date string to NSDate in objective c/iOS - iphone

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.

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

NSDateFormatter is returning null date

Below is my code
NSString *dateandtime =[NSString stringWithFormat:#"%#",self.eventtime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateandtime];
if i print my self.event name i am getting the output as Mon, 7 Jan 2013 15:30:00 CST but when i pass this string in NSDateFormatter i am getting null value i don't know why i tried many combinations of date formats, but i cant solve it, can anyone help me
i solved my problem as Per Prateek comments, now its working for me...Thanks Prateek.
in your self.event, you are getting CST, it should be time zone for example +0530
Use :
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
Use this code...
- (NSString *)formattedDate:(NSString*)dateSte {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:gmtZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateSte];
[dateFormatter setDateFormat:#"EEE,MMM d,''yyyy"];
NSString *strDate = [dateFormatter stringFromDate:date];
return strDate;
}
Use the below method for getting the NSDate from NSString date
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [self convertStringToDate:dateandtime];
Paste this below method in your .m file...
-(NSDate *)convertStringToDate:(NSString *) date {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss 'CST'"];// set format here format in which the date should be
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
For More Information About NSDate component see this post of My Blog NSDate-formate-info-for-iphone-sdk

NSDate from string

I have a string "2012-09-16 23:59:59 JST"
I want to convert this date string into NSDate.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 JST"];
NSLog(#"%#", capturedStartDate);
But it is not working. Its giving null value. Please help..
When using 24 hour time, the hours specifier needs to be a capital H like this:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
Check here for the correct specifiers : http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
However, you need to set the locale for the date formatter:
// Set the locale as needed in the formatter (this example uses Japanese)
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ja_JP"]];
Full working code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ja_JP"]];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 JST"];
NSLog(#"Captured Date %#", [capturedStartDate description]);
Outputs (In GMT):
Captured Date 2012-09-16 14:59:59 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 GMT-08:00"];
NSLog(#"%#", capturedStartDate);
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// end
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

dateFromString always returns null with dateformatter

I have the following code:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setFormatterBehavior:NSDateFormatterBehaviorDefault];
[df dateFromString:#"2011-04-12 10:00:00"];
In which it always generates a null date. Why is this?
If the device is set to AM/PM time and requested string format is set to #"yyyy-MM-dd HH:mm:ss" dateFromString will return nil.
If you set the locale to #"en_US" conversion will return correct date.
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:#"2013-02-14 09:30:00"];
Are you doing this in a background thread? I had weird experiences with NSDateFormatter when not being used in the ui-thread. Anyway, here's the method I use, should work for you:
+ (NSDate*)parseDate:(NSString*)inStrDate format:(NSString*)inFormat {
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setLocale:[NSLocale systemLocale]];
[dtFormatter setDateFormat:inFormat];
NSDate* dateOutput = [dtFormatter dateFromString:inStrDate];
[dtFormatter release];
return dateOutput;
}
I had the same issue. I started by double-checking that the string used for conversion was valid by using this resource.
I then had a thought that it might be the locale, so I tried the following:
[dateFomatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_AU"]]
It worked like a charm. Hope this helps.
I just ran this code and it worked for me.
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *theDate = [df dateFromString:#"2011-04-12 10:00:00"];
NSLog(#"date: %#", theDate);
The output was: date: 2011-04-12 17:00:00 +0000
Yes, agreed with #Jan Gressmann, this is so wierd! I am using the NSDateFormatter on non-ui thread as well, and dateFromString selector returns null sometimes... For example, I have this code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setLocale:usLocale];
[dateFormat setDateFormat:#"dd MMM yy HH:mm:ss"];
NSDate *startDate = [dateFormat dateFromString:str_StartDate];
NSDate *endDate = [dateFormat dateFromString:str_endDate];
Output in console:
(lldb) po str_StartDate
(NSString *) $7 = 0x06da2180 03 SEP 2012 10:00:00
(lldb) po str_endDate
(NSString *) $8 = 0x06d3a810 08 SEP 2013 10:00:00
(lldb) po startDate
(NSDate *) $9 = 0x00000000 <nil>
(lldb) po endDate
(NSDate *) $10 = 0x06db05b0 2013-09-08 02:00:00 +0000
This is really wierd...
I was getting similar issue with iOS8 (XCode 6), I did following changes in NSDateFormatter -
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *formattedDate = [dateFormatter dateFromString:dateString];
dateFromString and stringFromDate return null because of device time format (ex: 12hr Or 24hr) and device time zone
Convert 12hr To 24hr
- (NSString*)getTimeIn24Hr:(NSString*)time12hrWithDate
{
NSDateFormatter* df12 = [[NSDateFormatter alloc] init];
[df12 setTimeZone:[NSTimeZone systemTimeZone]];
[df12 setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSDate* dateTime12hr = [df12 dateFromString:time12hrWithDate];
NSDateFormatter *df24 = [[NSDateFormatter alloc]init];
[df24 setLocale:[NSLocale systemLocale]];
[df24 setDateFormat:#"HH:mm:ss"];
NSString * time24HrOutput = [df24 stringFromDate:dateTime12hr];
return time24HrOutput;
}
Convert 24hr To 12hr
- (NSString*)getTimeIn12Hr:(NSString*)time24hrWithDate
{
NSDateFormatter* df24 = [[NSDateFormatter alloc] init];
[df24 setLocale:[NSLocale systemLocale]];
[df24 setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* dateTime24hr = [df24 dateFromString:time24hrWithDate];
NSDateFormatter *df12 = [[NSDateFormatter alloc]init];
[df12 setDateFormat:#"hh:mm a"];
NSString * time12HrOutput = [df12 stringFromDate:dateTime24hr];
return time12HrOutput;
}
I have the similar issue with you, dateFromString: success in ios simulator, while in iPhone, it always return NULL, after I add the below code attached with dateFormatter, it success in iPhone ad simulator:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
Try to set the both date formatters in the same format while converting (date to string) and (string to date).In the following format
[formatter1 setDateFormat:#"MM/dd/yyyy hh:mm:ss a"]
[formatter2 setDateFormat:#"MM/dd/yyyy hh:mm:ss a"]
You can use this format "yyyy-MM-dd hh:mm:ss".
The key is using downcased hh rather than HH to format hour when you use swift2.3 on iOS10
Hope that could be useful.