iOS中的日期和时间格式

时间:2013-02-12 15:28:05

标签: ios string nsdateformatter

我知道NSDateFormatter,但我希望它像“昨天12:35”而且我只是得到你连接两个日期字符串?

[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setLocale: [NSLocale autoupdatingCurrentLocale]];

[dateFormatter setDoesRelativeDateFormatting:YES];

[dateFormatterTime setLocale:[NSLocale systemLocale]];
[dateFormatterTime setDateFormat:@"HH:mm"];

NSString *dateString = [NSString stringWithFormat:@"%@ %@", [dateFormatter stringFromDate:dateParsed], [dateFormatterTime stringFromDate:dateParsed ] ];

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

这是我能通过尊重语言环境所做的最好的事情。

NSDateFormatter *dateFormatter = NSDateFormatter.new;
dateFormatter.doesRelativeDateFormatting = YES;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;

NSDate *today = NSDate.new;
// "Today, 11:40 AM"
NSLog(@"%@", [dateFormatter stringFromDate:today]);

NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
// "Tomorrow, 11:40 AM"
NSLog(@"%@", [dateFormatter stringFromDate:tomorrow]);

NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-(60*60*24)];
// "Yesterday, 11:40 AM"
NSLog(@"%@", [dateFormatter stringFromDate:yesterday]);

NSDate *dayAfter = [NSDate dateWithTimeIntervalSinceNow:(60*60*24)*2];
// "2/14/13, 11:40 AM"
NSLog(@"%@", [dateFormatter stringFromDate:dayAfter]);