如何在UTC项目中获取UTC时间?

时间:2012-11-12 11:25:30

标签: iphone objective-c datetime timezone utc

如何让我的国家/地区的UTC时间用于iPhone开发?

3 个答案:

答案 0 :(得分:1)

当您想要设置TimeZone和日期格式时,以下是Code,Just Call下面的方法。

-(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.
}

答案 1 :(得分:0)

-(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

答案 2 :(得分:0)

[NSDate date]为您提供当前时间点,可以打印在UTC,GMT,EST或任何其他时区。 (表示方式,当前时间点是自UTC参考日期以来的秒数。)

要以UTC为单位获取当前时间点,请获取NSDateFormatter并将其配置为使用UTC时区输出时间戳。