ios - NSDate 格式没有显示预期的内容

标签 ios nsdate xcode4.5 nsdateformatter

我在将日期打印为 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/

相关文章:

ios - 在 TableView 单元格 iOS 中设置 UILabels 的值

ios - 使用 Swift 的 Twitter API 无效 URLRequestWithMethod

iphone - NSDate - iPhone 上的 GMT

ios - 无论用户身在何处,如何找出用户的8 :00 PM,?

c++ - Cocos2d-x-clang : error: linker command failed with exit code 1 (use -v to see invocation)

ios - Xcode : Event not created in Calendar : issue With Timezone?

ios - NSURLSessionConfiguration TLSMinimumSupportedProtocol 看起来在 iOS 上不起作用

ios - Firebase首次打开事件不正确

ios - 找到一周中依赖于语言环境的第一天

c++ - XCode 中的 LLVM-GCC ASM 到 LLVM