What is the NSFormat required for a string like this?
Fri, 15 Jun 2012 15:37:38 GMT
I'm trying to create a NSDate object from it. thanks
Use the following
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
NSDate *date = [formatter dateFromString:#"Fri, 15 Jun 2012 15:37:38 GMT"];
NSLog([date description]); //2012-06-15 15:37:38 +0000
Related
Needs some help with the following code:
self.dateTimestamp = userInfo[#"timestamp"];<- Returns UTC Time April 09 2018 11:09
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM dd YYYY HH:mm"];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setCalendar:[NSCalendar currentCalendar]];
NSDate *dte = [dateFormat dateFromString:self.dateTimestamp];
NSLog(#"Date: %#", dte); <-Date: Sun Dec 24 11:09:00 2017
Why do I have a date disparity.
Just replace YYYY by yyyy:
NSString *dateTimestamp = #"April 09 2018 13:07";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM dd yyyy HH:mm"];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setCalendar:[NSCalendar currentCalendar]];
NSDate *dte = [dateFormat dateFromString:dateTimestamp];
NSLog(#"Date: %#", dte);
Result : Mon Apr 9 13:07:00 2018
But When using
[dateFormat setDateFormat:#"MMMM dd YYYY HH:mm"];
It's logging : Sun Dec 24 13:07:00 2017
I am getting response from service that
"created_at" = "Thu Jul 06 07:26:23 +0000 2017";
i want to convert the string into Nsdate format
i do not know how to do it
Try this:
NSString *dateString = #"Thu Jul 06 07:26:23 +0000 2017";
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"EEE MMM dd hh:mm:ss Z yyyy";
date = [ dateFormatter dateFromString:dateString];
Use this code
(source::http://nsdateformatter.com)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:#"Thu Jul 06 07:26:23 +0000 2017"]];
NSLog(#"date is %# ",date);
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d HH:mm:ss Z yyyy"];
NSDate *date = [f dateFromString:#"Thu Jul 06 07:26:23 +0000 2017"];
Try this code:
NSString *dateString = #"2018-07-27 03:08:10";
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-mm-dd hh:mm:";
date = [ dateFormatter dateFromString:dateString];
ObjectiveC implemetation
NSString *dateString = #"Thu Jul 06 07:26:23 +0000 2017";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date =[[NSDate alloc] init];
dateFormatter.dateFormat = #"EEE MMM DD hh:mm:ss Z yyyy";
date = [dateFormatter dateFromString: dateString]; //generates date from dateString
Swift implementation goes below
let dateString = "Thu Jul 06 07:26:23 +0000 2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM DD hh:mm:ss Z yyyy"
dateFormatter.date(from: dateString) //"Jan 6, 2017, 12:56 PM"
I've got this kind of date: Sun, 30 Jun 2013 16:00:00
I've created this NSDateFormatter:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss"];
but when I use it with dateFromString it returns nil.
What's wrong?
Thank you!
Does this fix it? (Maybe your device is in a locale that doesn't understand Sun or Jun)
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
What's the correct format to parse this string:
"Mon, 14 Mar 2011 15:36:00 GMT"
into a NSDate using NSDateFormatter?
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *myDate = [dateFormatter dateFromString:strPubDate];
NSString *dateStr = [NSString stringWithString:#"Mon, 14 Mar 2011 15:36:00 GMT"];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss zzzz"];
NSDate *date = [dateFormat dateFromString:dateStr];
I am getting nil returned for the date variable in the below code. I can't find any problem with the date format, can anyone help?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = [[NSString alloc] initWithFormat:#"%#", #"Mon Apr 05 04:37:28 UTC 2010"];
NSDate *date = [formatter dateFromString:dateString];
Try
NSDate *date = [formatter dateFromString:#"Mon Apr 05 04:37:28 GMT 2010"];
or
NSDate *date = [formatter dateFromString:#"Mon Apr 05 04:37:28 +0000 2010"];