I am attempting to format an NSString which looks like:
#"2012-07-19T02:58:33Z"
to an NSDate with the following format:
#"MMMM dd, yyyy HH:mm a"
The NSDateFormatter always returns nil and I think it is because it cannot convert the NSString in it's current format to the desired one. Here is my code:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *date = [formatter dateFromString:dateString];
Do I need to store the date in a differant format? Can I make this conversion possible with the current NSString?
Thanks!
Your dateFormatter doesn't match your dateString... Try this
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *date = [formatter dateFromString:dateString];
[formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
dateString=[formatter stringFromDate:date];
now dateString contains "July 19, 2012 02:58 AM"
Hope this helps
Try it.
NSString *myDateString=#"2012-07-19T02:58:33Z";//#"2009-07-06T17:42:12";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *myDate = [[NSDate alloc] init];
myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"MyDate is: %#", myDate);
Your problem has already been answered.
Store your string in a NSDate with the format of the original string:
NSString *originalDateString = #"2012-07-19T02:58:33Z";
NSDateFormatter *originalFormatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *originalDate = [formatter dateFromString:originalDateString];
NSDateFormatter *newFormatter = [formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
NSString *newDateString = [formatter stringFromDate:originalDate];
Related
I have a date string like this "2013-09-05T06:40:19Z" from server
i want to convert it into date which format should be same as my iphone follow .
how to do this ?
when i do this ?
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MMM dd yyyy"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:time];
NSLog(#"%#",date)//getting null here
i am getting null in date.
NSString *dateStr = #"2013-09-05T06:40:19Z";
NSString *outDateFormate = #"MMM dd yyyy";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *intime = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:outDateFormate];
NSString *dateWithNewFormat = [dateFormat stringFromDate:intime];
Since you are getting the date in specified format so you need to define your date formatter accordingly
NSString *gameDateString = #"2013-09-05T06:40:19Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *gameDate = [dateFormatter dateFromString:gameDateString];
NSLog#("** The date is %#",gameDate);
// Now convert date to string in the format which you required.
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *dateString = [dateFormatter stringFromDate:gameDate];
NSLog#("** The date string is %#",dateString);
Hope this helps.
NSString *str=#"2013-09-05T06:40:19Z";
str=[[str stringByReplacingOccurrencesOfString:#"T" withString:#" "]stringByReplacingOccurrencesOfString:#"Z" withString:#""];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:str];
[dateFormatter1 setDateFormat:#"MMM dd yyyy"];
NSString *newDate=[dateFormatter1 stringFromDate:date];
NSLog(#"%#",newDate)
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];
I am struggling a bit to convert the UTC time to the date format what i want.
2012-03-10T09:30:00Z
needs to be converted to the below format
3:00:00pm April 19, 2012
Could you someone please help me to resolve this?
I tried like below,
NSString *formatted;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:#"HH:mm:ss EEEE, MMM d"];
// Here datetime= 2012-03-10T09:30:00Z
NSDate *date = [NSDate dateWithTimeIntervalSince1970: [datetime intValue]];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
But, its printing wrong date like "06:03:32 Thursday, Jan 1". Please correct me.
UPDATED: I am having another problem, its not giving me the correct time, but its giving the correct date though. My Code is below.
NSString *formatted;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:#"HH:mm:ss EEEE, MMM d"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [df dateFromString:datetime];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
Thank you!
To convert an ISO date string to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [df dateFromString:date_str];
You can then convert it to your local time zone:
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:#"HH:mm:ss EEEE, MMM d"];
In your code, your [datetime intValue] is probably incorrect.
I have struggling with to convert nsstring to nsdate format. I have a date in nsstring format like (2011-08-25 18:30:00 +0000), but i want to change it in nsdate format (08/26/2011). How to change it? Can any one help me please? Thanks in advance... I have spend one day to solve this but i cant find out the solution.. Please help me friends....
This is my code,
NSString *dateString = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"Converted date is %#",dateFromString);
Output is,
Converted date is (null)
Yuva.M
NSString *dateWithInitialFormat = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
From NSLog: dateWithNewFormat: 08/25/2011
See Date Format Patterns
You need to use an NSDateFormatter and set the date format to match how you're storing it, like the below.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: myDateString];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM dd, yyyy"];
NSDate* d = [df dateFromString:#"January 06, 1992"];
NSLog(#"%#", d);
NSDate *dispalyDate = d;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM"];
NSString *str = [dateFormat stringFromDate:dispalyDate];
NSLog(#"%#", str);
I have allocated a date formatter, and I am trying to format a string which contains a date:
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *date = [[stories objectAtIndex:storyIndex] objectForKey:#"date"];
NSDate *formattedDate = [formatter dateFromString:date];
NSLog(#"%#", formattedDate);
As you can see I am 'NSLogging' the formattedDate to see if it had worked or not. Unfortunately it has not worked and therefore is returning (NULL).
Please could you tell me what I am doing wrong.
Thanks in advanced.
The string "Fri, 10 Dec 2010 23:48:36 +0000" is not a "short style".
To parse that reliably, set the locale and an explicit date format:
formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[formatter setLocale:locale];
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSString *date = [[stories objectAtIndex:storyIndex] objectForKey:#"date"];
NSDate *formattedDate = [formatter dateFromString:date];
NSLog(#"%#", formattedDate);
Edit:
To then display the NSDate formattedDate in the format "Friday 10 December 2010", add this code after the above:
NSDateFormatter *displayFormatter = [[NSDateFormatter alloc] init];
[displayFormatter setLocale:locale];
[displayFormatter setDateFormat:#"EEEE dd MMMM yyyy"];
NSString *displayDate = [displayFormatter stringFromDate:formattedDate];
NSLog(#"displayDate = %#", displayDate);
[displayFormatter release];
For an explanation of the locale see Apple QA1480.
For an explanation of the date format see Unicode Date Format Patterns.