Help with date formats - objective-c

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

Related

How do I represent it with date for "dd MMM yyyy", means "11 JUN 2019"?

I have date string like this "11 June, 2019 16:35:21". When I try to convert the string to NSDate it returns nil.
NSString str =#"11 June, 2019 16:35:21";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"dd MMM yyyy"];
NSDate *date = [formatter dateFromString:str];
NSString *string = [[formatter stringFromDate:date] uppercaseString];
Is there any other way to convert it? Or where I'm doing wrong?
use date format as like
NSString str =#"11 June, 2019 16:35:21";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:locale];
// input dateformat #"11 June, 2019 16:35:21";
[formatter setDateFormat: #"dd MMMM, yyyy HH:mm:ss"];
NSDate *date = [formatter dateFromString:str];
// output dateformat for e.g #"11 Jun 2019
[formatter setDateFormat: #"dd MMM yyyy"];
NSString *string = [[formatter stringFromDate:date] uppercaseString];
The NSDateFormatter uses Unicode Technical Standard.
this site has a great documentation:
Date_Format_Patterns
e.g. "11 June, 2019 16:35:21" can be parsed with:
[dateFormatter setDateFormat:#"dd MMMM, yyyy HH:mm:ss"]
Karthik is right, first, you have to convert date string with #"dd MMMM, yyyy HH:mm:ss" format to NSDate, then convert it to your desired format #"dd MMM yyyy".
Hope so following code'll work.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:locale];
[formatter setDateFormat: #"dd MMMM, yyyy HH:mm:ss"];
NSString *str = #"11 June, 2019 16:35:21";
NSDate *date = [formatter dateFromString:str];
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: #"dd MMM yyyy"];
NSString *string = [[formatter stringFromDate:date] uppercaseString];

ObjC Date always returns December 25 2017

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

How to convert NSstring to Nsdate

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"

Have troubles with NSDateFormatter while parsing

Here is the date sample: Wed, 13 May 2015 16:10:00 CEST
My formatter aint working
#"EEE, dd MMM yyyy HH:mm:ss vvvv"
What should i change?
UPD: full code
NSString* formattedDayWithString(NSString* date){
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
NSString *localeId = [locale displayNameForKey:NSLocaleIdentifier
value:[locale localeIdentifier]];
SLog(#"%# date:%#", localeId, date);
[inputFormatter setLocale: locale];
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss vvvv"];
NSDate *formattedDate = [inputFormatter dateFromString: date];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
[outputFormatter setDateFormat:#"d'.' MMMM yyyy"];
NSString* dateStr = [[NSString alloc] initWithFormat: #"%#",[outputFormatter stringFromDate:formattedDate]];
return dateStr;
}
"CEST" is a "short specific non-location format" and the corresponding
date field symbol is "z", not "v":
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
In addition, you might have to set the date formatter locale explicitly to "en_GB"
as explained in Nsdateformatter + Timezone issue.

NSDate and NSDateFormatter return invalid CFStrinfRef

I've one problem with NSDate and NSDateFormatter.
From this NSString
NSString *startDate = #"Thu, 19 Apr 2012 13:09:56 +0000";
I would obtain a date localized with current locale like this "Thu, 19 Apr 2012 13:09".
I've elaborate this code but the 'endDate' string return nil (within the debugger obtain 'invalid CFStringRef').
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:startDate];
[dateFormatter setDateFormat:#"EEE dd MMM yyyy HH:mm"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *endDate = [dateFormatter stringFromDate:dateFromString];
Where is the bug?
Alex.
It seems that the code executed in a non-US locale. I use non-US locales. I have the same nil output also.
It seems like we need to setLocale: before setDateFormat:.
Below code works ok:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
Ya its working correctly..Try removing autorelease from your dateFormatter and then try it again.
And i hope this link is same as yours
Invalid CFStringRef issue
Try this answer and change your format option with "ZZ" and not "ZZZ".