-1

我有这段代码适用于 iOS5,但我只是测试

NSDate *now = [NSDate date];
NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];

有谁知道重写这个的简单解决方案。日期格式应为 dd mm yyyy

4

2 回答 2

7

将日期格式化为字符串的正确方法是使用NSDateFormatter。您可以使用该方法将样式设置为适合用户当前语言环境的样式-setDateStyle:,或者使用-setDateFormat:.

于 2013-06-18T20:12:38.153 回答
1
Try this one , Definitely will get Proper solution:


NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];//as per your requirement

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"]; //as per your requirement

NSDate *now = [[NSDate alloc] init];

NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];

NSLog(@"theDate:%@ "theTime:%@" , theDate, theTime);
于 2013-07-19T09:15:59.777 回答