xcode NSDateFormatter 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5673994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:50:17  来源:igfitidea点击:

NSDateFormatter Question

iphoneobjective-cxcodetimensdateformatter

提问by h4wkeye

i am using the following code to get the current time..

我正在使用以下代码来获取当前时间..

NSDate *myDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0200"]];

// Set date style:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];

NSString *GMTDateString = [dateFormatter stringFromDate: myDate];

My problem is : I want the time in HH:MM:SS:(milliseconds if possible)

我的问题是:我想要 HH:MM:SS:(如果可能的话,毫秒)

When i run the project, it displays the date and the right time but it also prints "MESZ" which i assume means Middle European System time in My language.

当我运行该项目时,它会显示日期和正确的时间,但它还会打印“MESZ”,我认为这意味着我的语言中的中欧系统时间。

How od i get rid of MESZ and print the time with miliseconds?

我如何摆脱 MESZ 并以毫秒为单位打印时间?

Any help is appreciated

任何帮助表示赞赏

thanks in advance

提前致谢

h4wkeye

h4wkeye

EDIT :

编辑 :

All i need now is an advice on how to print the milliseconds as well! Thanks for all your helpful answers

我现在需要的只是关于如何打印毫秒的建议!感谢您提供所有有用的答案

采纳答案by Robin

Use the "setDateFormat:" of NSDateFormatter with your custom attributes, an explanation of how to use it can be found here: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

使用带有自定义属性的 NSDateFormatter 的“setDateFormat:”,可以在此处找到有关如何使用它的说明:http: //developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DataFormatting/Articles/ dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

回答by Alan

Looks like nobody has answered you about milliseconds... use "S" in your format string, like this:

看起来没有人回答你大约毫秒......在你的格式字符串中使用“S”,如下所示:

[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];

回答by EXC_BAD_ACCESS

You set one more property for dateFormatter like this

您像这样为 dateFormatter 设置了另一个属性

[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];