我需要日期作为字符串,而不是时间,它必须本地化。
因此,例如 USA 应该是Sep 25 2009,但对于新西兰来说应该是25 Sep 2009。我可以通过指定格式“MMM dd YYYY”将日期转换为字符串,但它没有本地化。
有任何想法吗?
我需要日期作为字符串,而不是时间,它必须本地化。
因此,例如 USA 应该是Sep 25 2009,但对于新西兰来说应该是25 Sep 2009。我可以通过指定格式“MMM dd YYYY”将日期转换为字符串,但它没有本地化。
有任何想法吗?
[NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterMediumStyle
timeStyle:0]
关键是将 setTimeStyle:kCFDateFormatterNoStyle 发送到 dateFormatter。这将设置 dateFormatter 以便它只格式化日期而不输出时间:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [NSLocale currentLocale]];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle]; // or whichever style...
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle];
NSString* dateString = [dateFormatter stringFromDate: [NSDate date]];
这给了我以下输出:
12/18/09