Objective c iOS6中的NSDate与iOS5中的不同

Objective c iOS6中的NSDate与iOS5中的不同,objective-c,ios5,ios6,nsdate,Objective C,Ios5,Ios6,Nsdate,我让这段代码为iOS5工作,但我只是测试了一下 NSDate *now = [NSDate date]; NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now]; 有人知道重写这个的简单方法吗。日期的格式应为 dd-mm-yyyy将日期格式化为字符串的正确方法是使用。您可以使用该方法将样式设置为适合用户当前区域设置的样式,或使用将格式设置为特定字符串。您发布的代码将导致在NSDate对象上调用description方法。d

我让这段代码为iOS5工作,但我只是测试了一下

NSDate *now = [NSDate date];
NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];
有人知道重写这个的简单方法吗。日期的格式应为
dd-mm-yyyy

将日期格式化为字符串的正确方法是使用。您可以使用该方法将样式设置为适合用户当前区域设置的样式,或使用将格式设置为特定字符串。

您发布的代码将导致在
NSDate
对象上调用
description
方法。
description
方法的输出仅用于调试,并且输出可能会随着时间的推移而改变(如您所见)。
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);