NSDateFormatter Question

♀尐吖头ヾ 提交于 2020-01-01 04:28:04

问题


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)

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.

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

Any help is appreciated

thanks in advance

h4wkeye

EDIT :

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


回答1:


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




回答2:


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

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



回答3:


You set one more property for dateFormatter like this

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


来源:https://stackoverflow.com/questions/5673994/nsdateformatter-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!