如何在UTC中获得iPhone开发的时间?
这是代码,只需在要设置时区和日期格式时调用下面的方法。
-(void)setDateFormat
{
NsDate myDate = [NSDate date];//here it returns current date of device.
//now set the timeZone and set the Date format to this date as you want.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *newDate = [dateFormatter stringFromDate:myDate];
// here you have new Date with desired format and TimeZone.
}
-(NSString *)UTCFormDate:(NSDate *)myDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate:myDate];
return dateString;
}
来源:https://stackoverflow.com/a/2615847/857865
[NSDate date]
为您提供了当前的时间点,可以在UTC,GMT,EST或任何其他时区打印。(在表示方面,当前时间点是自UTC中的参考日期以来的秒数。)
要获取UTC中的当前时间点,请获取NSDateFormatter
并将其配置为使用UTC时区输出时间戳。