Have troubles with NSDateFormatter while parsing - objective-c

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.

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

Convert between date formats in Objective-C

I am trying to convert a date into a different format. I'm receiving my date as an NSString with the following format: EEE MMM dd HH:mm:ss ZZZ yyyy, and am attempting to change it to this format: dd-mm-yy. However, I am not able to get it in desired format.
This is my current code:
NSString *dateStr = [NSString stringWithFormat:#"%#",[dict valueForKey:#"createdOn"]];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
dateFormatter.timeZone = gmt;
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
NSDate *dateFromString = [dateFormatter dateFromString:dateStr];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *newDateString = [dateFormatter2 stringFromDate:dateFromString];
The locale en_US doesn't understand the IST time zone abbreviation. But en_IN does:
NSString *dateStr = #"Tue Mar 24 08:28:48 IST 2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"];
NSDate *dateFromString = [dateFormatter dateFromString:dateStr];
As John Skeet points out, the issue probably stems from the fact that IST is not unique. IST stands for both Israel Standard Time, and India Standard Time. Thus, when you specify India locale, it makes a reasonable assumption, but for US locale, it is understandably confused.
Unrelated, but make sure to use MM rather than mm in your output formatter:
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MM/yyyy"];
NSString *newDateString = [dateFormatter2 stringFromDate:dateFromString];

NSDate formatter issue with Twitter JSON API

I'm trying to format JSON response date value:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//Wed Dec 01 17:08:03 +0000 2010
[df setDateFormat:#"eee, MMM dd HH:mm:ss ZZZZ yyyy"];
NSDate *date = [df dateFromString:[twitter objectForKey:#"created_at"]];
[df setDateFormat:#"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];
I follow instructions of this question:
Date/Time parsing in iOS: how to deal (or not deal) with timezones?
Actually, date value in JSON response is:
created_at":"Mon, 28 May 2012 11:20:29 +0000"
Date variable is nil when receiving value by [df dateFromString:[twitter objectForKey:#"created_at"]];
Many thanks
try this format if the month is abbreviated (Sept) :
[df setDateFormat:#"EEE, d MMM y HH:mm:ss Z"];;
and
[df setDateFormat:#"EEE, d MMMM y HH:mm:ss Z"];
if the month name is written out (September).
the full list of format specifiers.
I also had to change the locale
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[df setDateFormat:#"EEE, d MMM y HH:mm:ss Z"];
NSDate *date = [df dateFromString:#"Mon, 28 May 2012 11:20:29 +0000"];
[df setDateFormat:#"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];
NSLog(#"%#",dateStr);
output
28/05/2012
i found that the "created_at" attribute in the twitter Json response is in this format:
Thu Apr 19 10:07:29 +0000 2012
so i used this formatter :
[df setDateFormat:#"EEE MMM d HH:mm:ss Z y"];
it's working fine ;)
To validate the format you're using, create a string from a date:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"eee, MMM dd HH:mm:ss ZZZZ yyyy"];
NSLog(#"%#", [df stringFromDate: [NSDate date]];
Compare this with the date from your JSON response and you should be able to see where your format is wrong.
In Swift
let df = NSDateFormatter()
df.dateFormat = "eee, MMM dd HH:mm:ss ZZZZ yyyy"
let dateStr = df.stringFromDate(NSDate())
println("\(dateStr)")
Right formatter value is:
[df setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
for:
created_at":"Mon, 28 May 2012 11:20:29 +0000"
"created_at" = "Sat Apr 05 06:31:57 +0000 2014";
Following Method Return How many time before twit aris.
-(NSString*)retrivePostTime:(NSString*)postDate{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"eee MMM dd HH:mm:ss ZZZZ yyyy"];
NSDate *userPostDate = [df dateFromString:postDate];
NSDate *currentDate = [NSDate date];
NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:userPostDate];
NSTimeInterval theTimeInterval = distanceBetweenDates;
// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];
// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];
// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1 toDate:date2 options:0];
NSString *returnDate;
if ([conversionInfo month] > 0) {
returnDate = [NSString stringWithFormat:#"%ldmth ago",(long)[conversionInfo month]];
}else if ([conversionInfo day] > 0){
returnDate = [NSString stringWithFormat:#"%ldd ago",(long)[conversionInfo day]];
}else if ([conversionInfo hour]>0){
returnDate = [NSString stringWithFormat:#"%ldh ago",(long)[conversionInfo hour]];
}else if ([conversionInfo minute]>0){
returnDate = [NSString stringWithFormat:#"%ldm ago",(long)[conversionInfo minute]];
}else{
returnDate = [NSString stringWithFormat:#"%lds ago",(long)[conversionInfo second]];
}
return returnDate;
}

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".

Help with date formats

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