NSDataFormatter dateFromString results in incorrect output - iphone

Here is my code :
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
logDate = [dateFormat dateFromString:[line substringToIndex:24]];
line consist of string : "Mon Feb 18 09:25:53 2013: FAILED : Configuration-Update :"
I get the date alone "Mon Feb 18 09:25:53 2013" and formatted it with #"EEE MM d HH:mm:ss yyyy" format.
I get an incorrect output : "2013-02-18 03:55:53 -0000" Time alone is printed incorrectly. I followed Date Format Patterns for specifying patters but still I am phasing this issue. I am not able to understand where it going wrong.. It would be helpful if someone finds the problem.
thanks in advance

Try this one
NSDate *date = [NSDate date];
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"EEE MM d hh:mm:ss yyyy"];
NSLog(#"%#",[formatter stringFromDate:date]);
NSLog(#"%#",date);
When you just log using NSLog you get the UTC format (UTC consists of offset value, in India its standard time +5.30)that is yyyy-MM-d HH:mm:ss z , z is the offset added or subtracted to or from current time based on the locality.
For example
NSLog(#"%#",[formatter stringFromDate:date]);
NSLog(#"%#",date);
Tue 02 26 05:38:00 2013 //"EEE MM d hh:mm:ss yyyy"
Tue 02 26 17:46:22 2013 //"EEE MM d HH:mm:ss yyyy"
2013-02-26 12:08:00 +0000 //just logging using NSLog
Hope this helps you

You'll need to set timezone for a NSDateFormatter when you print resulting date like this:
[formatter setTimeZone:[NSTimeZone localTimeZone]];

You need to use as :
NSString *line=#"Mon Feb 18 09:25:53 2013: FAILED : Configuration-Update :";
NSDateFormatter *dateFormat=[NSDateFormatter new];
[dateFormat setDateFormat:#"EEE' 'MM' 'd' 'HH:mm:ss' 'yyyy"];
NSDate *logDate = [dateFormat dateFromString:[line substringToIndex:24]];
NSLog(#"logDate->%#",logDate);

If we take the log of the NSdate object that will show the date with respect to the timezone +0000 only. for that we need to format the date to our local timezone. like this..
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
logDate = [dateFormat dateFromString:#"Mon Feb 18 09:25:53 2013"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
NSLog(#"Date %#",[dateFormat stringFromDate:logDate]);

thanks to all.. it worked by changing the code this way ..
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease];
[dateFormat setDateFormat:#"EEE MM d HH:mm:ss yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
return [dateFormat dateFromString:dateString];

Related

iOS - Converting string to non-en-GB locale NSDate returns nil [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSString to NSDate
I have a date in format Mon Jan 14 14:00:00 CET 2013 I try to convert it to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
but it doesn't work and my date is nil
Input data is in en-GB locale, my device's locale is nb-NO
Any suggestions?
You're missing day in your format:
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
If it was not a typo, then next thing is to set proper locale so formatter will recognise CET timezone, for example en-GB will fix that:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
// that will fix the problem with not recognized CET timezone
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en-GB"]];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
Try to use this function
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
I hope this will helps u.
I think your Time Zone is wrong. Just use this code , it will work Perfectly :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 EDT 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date :: %#",date);
It will log Output as :
date :: 2013-01-14 18:00:00 +0000
EDIT :
I found Something for you : NSDateFormatter doesn't parse some timezones
You can solve this by using en_GB Locale , as stated : "These abbreviations do still work with the en_GB locale" in Working with Date and Time in Cocoa .
CET is not recognised
Try this :-
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"%#",date);
Hope it helps you
Simply "CET" is not a recognized time zone by NSDateFormatter.
Also the date/tine is over specified, best to not try include the day or week (Mon).
Here is an example that demonstrates working code with a recognized timezone:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 EST 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date: %#", date);
NSLog output
date: 2013-01-14 19:00:00 +0000
NSLog(#"abbreviationDictionary: %#", [NSTimeZone abbreviationDictionary]);
does show
CET = "Europe/Paris";
so this looks like an Apple bug in NSDateFormatter.
Report the bug at: Apple Bug Reporter
You can use:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
If you still want to custom your date format try this one:
yyyy-MM-dd HH:mm:ss ZZZ
Because can't invent your own formatted string syntax and expect it to work; you need to actually use a documented format as the documentation points it out : Formatters in OS X v10.8 and iOS 6.0 use version tr35-25.
-> https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
If you are curious: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Can't convert NSString to NSDate object

This is an example result to string from my server:
Sat, 24 Dec 2011 14:33:43 +0000
I'm trying to convert it to NSDate and I keep getting null.
This is my code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss +0000"];
NSDate *date = [dateFormat dateFromString:dateString];
Why do I get null?
It works fine here. You probably want your format to be this tho:
#"EEE, dd MMM yyyy HH:mm:ss z"
Otherwise it'll fail if the server returns a timezone that's not GMT.
try this
NSString *dateString =#"Sat, 24 Dec 2011 14:33:43 +0000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"%#",date);
Something else must be wrong with your code. Check where you allocate or assign dateString. That might be null to begin with.
Just tried your code with the string you said you're getting from the server and it's working fine. I even tried what mvds said about using [dateFormat stringFromDate:date] that's also returning the correct date

Date Format Problem

How to get this format of date from NSString;
Wed, 22 Jun 2011 12:36:00 +0100 to Wed, 22 Jun 2011.
Thanks
Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];
The minimalistic version is E, d MMM y or to specify 2 digit days and 4 digit years E, dd MMM yyyy. The Date Formatter uses the Unicode Technical Standard #35.

NSDateFormatter dateFromString issue

I am having a trouble converting NSString to NSDate.
I have a date string like 'Mon Dec 13 14:55:05 +0800 2010', here is my code:
NSString *dateString = [NSString stringWithFormat:#"Mon Dec 13 14:55:05 +0800 2010"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE MMM dd hh:mm:ss zzz yyyy"];
NSDate *date = [dateFormat dateFromString:dateString];
Odd, It works fine yesterday, but I'm getting nil now.
Any suggestions would be greatly appreciated. Thanks.
24-hour notation needs an HH string representation instead of hh so you should have
#"EEE MMM dd HH:mm:ss zzz yyyy"

How to convert NSDate to german Date?

I have implemented one iphone application in which I want to convert NSDate to NSString but in german format.
Can you give me some idea about that.
I am using below code.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[eventInfo valueForKey:#"startdat"] intValue]];
//2011-05-01 21:04:00 +0000(I am geeting this date)
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
NSLocale *nl_NL = [[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"];
[formatter1 setDateFormat:#"E,dd MMM yyyy"];
[formatter1 setLocale:nl_NL];
NSString *stringFromDate1 = [formatter1 stringFromDate:date];
[formatter1 release];
[nl_NL release];
//I am getting stringFromDate1 = "Mo.,02 Mai 2011" value.(wrong output)
Please give me idea
Use for example
[formatter1 setDateStyle:NSDateFormatterLongStyle];
instead of setDateFormat:.
The problem is that the time zone is taken into account when the NSDateFormatter is formatting the date. If you want the NSDateFormatter to format the exact same date as the NSLog'd version, you need to explicitly set the time zone of the formatter.
[formatter1 setDateFormat:#"E,dd MMM yyyy"];
[formatter1 setLocale:nl_NL];
[formatter1 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
I am not sure (from your question), what is your expected output. But assuming that you are looking for the full day of week description, try this date format:
[dateFormatter setDateFormat:#"EEEE, dd MMM yyyy"];
It gives the output:
Sonntag, 01 Mai 2011 (using de_DE locale)
In general, the number of characters determine the size of date field:
eg. Input date = 2011-05-01 Sunday
1-character = 1-digit/character number or word (if number/word can't be 1 character long then abbreviation or fullname is displayed).
[dateFormatter setDateFormat:#"E, d M y"]; // Sun, 1 5 2011
2-character = 2-digit/character number or word (if number/word can't be 2 character long then abbreviation is displayed).
[dateFormatter setDateFormat:#"EE, dd MM yy"]; // Sun, 01 05 11
3-character = 3-digit/character number or word, or abbreviation (generally).
[dateFormatter setDateFormat:#"EEE, ddd MMM yyy"]; // Sun, 001 May 2011
4-character = full name (generally).
[dateFormatter setDateFormat:#"EEEE, dddd MMMM yyyy"]; // Sunday, 0001 May 2011
Here's the weird part though, if you specify 5 E's, you get an rather unexpected output:
[dateFormatter setDateFormat:#"EEEEE, ddddd MMMMM yyyyy"]; // S, 00001 M 2011
For date formatting, I find the the following reference table very useful.
http://www.unicode.org/reports/tr35/#Date_Field_Symbol_Table
Good luck