2

如何在 UTC 中获取我的国家/地区时间以进行 iPhone 开发?

4

3 回答 3

1

这是代码,当您想设置时区和日期格式时,只需调用下面的方法。

-(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.
}
于 2012-11-12T12:28:33.823 回答
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;
}

Source: https://stackoverflow.com/a/2615847/857865

于 2012-11-12T11:29:00.207 回答
0

[NSDate date] gives you the current point in time, which may be printed in UTC, GMT, EST or any other timezone. (Representation-wise, the current point in time is the number of seconds since a reference date in UTC.)

To get the current point in time in UTC as a string, get an NSDateFormatter and configure it to output a timestamp using the UTC timezone.

于 2012-11-12T11:30:23.323 回答