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
Related
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];
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
One simple thing on conversion from NSString to NSDate. How can I convert Mon, 27 August 2012 01:30 AM to NSDate in this same format. I tried with NSDateFormatter. But I am not getting it in this required format. Can anyone help? This is what I tried.
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE,dd MM yyyy HH:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
NSDateFormatter is to specify the format that will appear in the date-string when extracting string from date or the format that is in the date-string when extracting date from string
So whenever you extract NSDate from a NSString, NSDate is always obtained in default date format(eg 2012-08-27 00:30:00 +0000)... only the when you extract NSString from NSDate, NSString can be obtained in desired(custom) format that you set in NSDateFormatter.
I hope this will help you sure!
NSDateFormatter *dateformater=[[[NSDateFormatter alloc] init]autorelease];
[dateformater setDateFormat:#"EEEE,dd MMMM yyyy HH:mm a"];
NSDate *todayTmp=[NSDate date];
NSString *conversionDate=[dateformater stringFromDate:todayTmp];
Note : (Upper case) HH for 24h time format, (Lower case) hh for 12h time format
NSString *myDateAsAStringValue = #"Mon, 27 August 2012 01:30 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSDate *myDate = [[NSDate alloc]init];
myDate = [df dateFromString:myDateAsAStringValue];
[df release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSString *strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
[dateFormatter release];
please use below code
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
your formatter is wrong
check this one
NSLog will return NSDate in a fixed format, i guess.
If we need Date in different format, we should have to format it via NSDateFormatter and get it as NSString.
Just a guess.
Don't forget to set the correct locale! If your device does not use an english locale NSDateFormatter can have problems to convert Mon and August into useful information because Mon is not the correct abbreviation for Monday in your language. For example in Germany the correct three letter abbreviation for Monday is Mon..
If you parse dates that have words in it you have to set the correct locale.
This should work:
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:posixLocale];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
EEE is the dateformatter code for a three letter weekday abbreviation.
hh is the dateformatter code for Hours between 1 and 12. HH means 0-23
MMMM is the full month, MM would be the numeric value (= 08) of the month.
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.
I use this code to process a date string coming in from a json feed:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle: NSDateFormatterLongStyle];
[formatter setFormatterBehavior: NSDateFormatterBehavior10_4];
[formatter setDateFormat: #"EEE, dd MMM yyyy HH:mm:ss +0000"];
so if I call
NSDate *date = [formatter dateFromString: #"Tue, 08 Sep 2009 19:21:27 +0000"];
I get back a usable date if my region format is United States or United Kingdoms, but if I set it to Germany it returns nil. I understand there are some differences in behaviors across different locales, but if I define a format shouldn't that correct for any inconsistencies?
Names like "Tue" and "Sep" are English. Other languages use different names.
If you want to be able to parse English dates independent of the device's region settings, set your DateFormatter's locale to en_US using the -setLocale: method.
Thanks fixed it up with:
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"US"] autorelease]];