我在将日期打印为 NSString 时遇到问题。我以这种方式在我的应用程序生命周期的某些点存储了当前日期和时间:
NSDate *current = [NSDate date];
long currentTime = [current timeIntervalSince1970];
现在,我需要在 UILabel 中显示这些日期。我试过这种方式:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:currentTime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy - HH:mm:ss"];
NSString *stringFromDate = [formatter stringFromDate:date];
但我没有得到正确的日期和时间。我可能缺少什么?
谢谢!
最佳答案
试试这个,
NSString *stringFromDate=[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
您还可以更改日期样式和时间样式。
关于ios - NSDate 格式没有显示预期的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13757008/