NSDateFormatter具有前导零

时间:2013-04-15 09:51:32

标签: objective-c cocoa

如何获得前导零的日,分和秒?

NSDateFormatter *lDateFormatter = [[NSDateFormatter alloc] init];
[lDateFormatter setDateFormat:@"LL-d-yyyy-H-m-s"];  
NSString *lTime = [lDateFormatter stringFromDate:[NSDate date]];
// Output : 04-3-2013-14-4-1

2 个答案:

答案 0 :(得分:4)

您添加日期说明符两次:LL-dd-yyyy-HH-mm-ss

NSDateFormatter *lDateFormatter = [[NSDateFormatter alloc] init];
[lDateFormatter setDateFormat:@"LL-dd-yyyy-HH-mm-ss"];
NSString *lTime = [lDateFormatter stringFromDate:[NSDate date]];
NSLog(@"Date: %@", lTime);      

将输出:Date: 04-15-2013-12-04-51

答案 1 :(得分:2)

使用MM-dd-yyyy-HH-mm-ss格式