This question already has answers here:
NSDateFormatter won't format strange date string
(3 answers)
Closed 9 years ago.
I have a string of the form "2013-05-12T15:38:20+00:00" but I can't work out why my date formatter won't convert it to an NSDate object. I have tried using "yyyy-MM-ddTHH:mm:ss+zz:zz" and a bunch of variations on it but no luck.
What would be the correct date format for this string?
Thanks
The correct date format for this string is "yyyy-MM-dd'T'HH:mm:ssz"
Thanks #Anupdas for pointing me to a similar question.
Use this format
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setDateFormat:#"yyyy-MM-dd'T'HH:mm:sszzz"];
NSLog(#"notDate =====> %#",[form dateFromString:#"2013-05-12T15:38:20+00:00"]);
NSDate *notDate = [form dateFromString:[#"" stringByAppendingFormat:#"2013-05-12T15:38:20+00:00"]];
NSLog(#"notDate =====> %#",[form dateFromString:#"2013-05-12T15:38:20+00:00"]);
I hope it helps you
Try this code
your string
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss+zz:zz"];
NSDate* date = [dateFormatter dateFromString:dateString];
Conversion of the NSString to a NSDate
Now here you change any format as you want
[dateFormatter setDateFormat:#"dddd - MMMM dd,yyy"];
NSString* myNiceLookingDate = [dateFormatter stringFromDate:date];
[dateFormatter release];
Related
This question already has answers here:
What format string do I use for milliseconds in date strings on iPhone?
(4 answers)
Closed 7 years ago.
I need to convert a date string "2015-03-10 00:00:00.000"(string from web service) to NSDate. I have used code like this
NSDateFormatter *fromDateFormat = [[NSDateFormatter alloc] init];
[fromDateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *outputDate = [fromDateFormat dateFromString:#"2015-03-10 00:00:00.000"];
outputDate is nil. I tried without last triple zeros, and it is working. but I can't remove that zeros, because it is from a service.
If you want convert string to date using This function
NSDate *current=[self convertStringToDate:data.strCreateDate formate:#"yyyy-MM-dd HH:mm:ss"];
Put this method in your projects
- (NSDate *)convertStringToDate: (NSString *)date formate:(NSString *)formate {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:formate];
return [dateFormat dateFromString:date];
}
This is helpfult to you..
This question already has answers here:
Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?
(6 answers)
Closed 7 years ago.
I have an NSString #"2015-05-24T20:10:00.000Z", and I am using an NSDateFormatter with the date format #"yyyy-MM-dd'T'HH:mm:ssZ", but it always returns nil.
I am using the following code, but the output is always (null).
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// NSString *input = #"2013-05-08T19:03:53+00:00";
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"]; //iso 8601 format
NSDate *date = [dateFormat dateFromString:[dictScheduleData valueForKey:#"scheduledOn"]]; // coming from the server 2015-05-24T20:10:00.000Z
NSLog(#"Date output: %#", date);
Try this dateFormat
#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
You have to match the string's date format. Use NSDateFormatter, then set the date format to match the string's date format. Then use dateFromString method.
to convert it to (5/24, 8:00PM), you can change NSDateFormatter's dateFormat to "M/d, h:mma", then use NSDateFormatter's method stringFromDate (the date you previously got, which should not be nil if done correctly). Then put that string in UILabel's text.
NSDate *currentTime = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[timeFormatter setLocale:[NSLocale currentLocale]];
NSString *DateString = [timeFormatter stringFromDate:currentTime];
This question already has answers here:
Changing date format on iOS
(2 answers)
Closed 8 years ago.
Was wondering if there is an easy conversion to take a date in the format dd/MM/yyyy and transform it to MM/dd/yyyy to display.
Current code tried is:
//in dd/MM/yyyy format string below.
NSString *stringToFormat = #"28/05/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:stringToFormat];
However, date will return nil in this situation. Any help would be appreciated. Thank you.
You'll have to "decode" and then "encode". You're currently trying to parse 28 as a month. Try this:
//in dd/MM/yyyy format string below.
NSString *stringToFormat = #"28/05/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:stringToFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
[dateFormatter stringFromDate:date];
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 8 years ago.
I don't why i'm getting one date less, when I'm converting a string from a date, i'm getting one date less, e.g. when i'm converting 18/06/2014, i'm getting 2014-06-17, Any idea why this problem, my codes are:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
This is what I'm getting wholly from the log: 2014-06-17 20:00:00 +0000
You will have to take the timezone into account. Your current timezone seems to be ahead of GMT. If you print the entire date with say a time stamp, then you will get the difference. So i suggest you add the timezone to the NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
NSLog(#"Date : %#", date);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
I make a request to the flickr api and it returns me the date in the following format
"2013-02-01T06:25:47Z"
How do i convert this into NSDate format??
It's a simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
You can use below function:
-(NSDate *)getDateFromString:(NSString *)pstrDate
{
NSDateFormatter *df1 = [[[NSDateFormatter alloc] init] autorelease];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dtPostDate = [df1 dateFromString:pstrDate];
return dtPostDate;
}
Hope, it will be helpful to you.
This function will accept your string date and return the NSDate value.
Cheers!