开发者

How to convert NSDate to german Date?

I have implemented one iphone application in which I want to convert NSDate to NSStr开发者_开发技巧ing 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜