iOS 将服务器时间转换为设备本地时间?
iOS convert server time to device local time?
我在将服务器时间(阿根廷)转换为设备本地时间时遇到了一些问题。
这是我当前的代码-
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
if (df == nil)
{
df = [[NSDateFormatter alloc]init];
}
df.dateFormat = @"HH:mm:ss";
NSDate* d = [df dateFromString:sourceTime];
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:@"ART"];//America/Argentina/Buenos_Aires (GMT-3)
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone]; //Asia/Kolkata (IST)
[df setTimeZone: sourceZone];
NSLog(@"sourceZone time is %@" , [df stringFromDate: d]);
[df setTimeZone: localTimeZone];
NSLog(@"local time is %@" , [df stringFromDate: d]);
NSLog(@"original time string was %@" , sourceTime);
return [df stringFromDate: d];
}
如果 sourceTime
字符串是 00:05:00
,这里是日志
2015-09-28 15:04:24.118 DeviceP[230:17733] sourceZone time is 15:35:00
2015-09-28 15:04:24.121 DeviceP[230:17733] local time is 00:05:00
2015-09-28 15:04:33.029 DeviceP[230:17733] original time string was 00:05:00
请注意,我得到的本地时间与我传递给该方法的时间字符串相同。 我看起来很不一样 post 就像 this and this。
任何帮助将不胜感激。
由于您的时间字符串在 ART 中,因此您应该在从字符串生成日期之前设置日期格式化程序的时区。表示喜欢关注
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = @"HH:mm:ss";
}
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:@"ART"];
[df setTimeZone: sourceZone];
NSDate *ds = [df dateFromString:sourceTime];
NSLog(@"sourceZone time is %@" , [df stringFromDate: ds]);
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(@"local time is %@" , [df stringFromDate: ds]);
return [df stringFromDate: d];
}
我在将服务器时间(阿根廷)转换为设备本地时间时遇到了一些问题。 这是我当前的代码-
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
if (df == nil)
{
df = [[NSDateFormatter alloc]init];
}
df.dateFormat = @"HH:mm:ss";
NSDate* d = [df dateFromString:sourceTime];
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:@"ART"];//America/Argentina/Buenos_Aires (GMT-3)
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone]; //Asia/Kolkata (IST)
[df setTimeZone: sourceZone];
NSLog(@"sourceZone time is %@" , [df stringFromDate: d]);
[df setTimeZone: localTimeZone];
NSLog(@"local time is %@" , [df stringFromDate: d]);
NSLog(@"original time string was %@" , sourceTime);
return [df stringFromDate: d];
}
如果 sourceTime
字符串是 00:05:00
2015-09-28 15:04:24.118 DeviceP[230:17733] sourceZone time is 15:35:00
2015-09-28 15:04:24.121 DeviceP[230:17733] local time is 00:05:00
2015-09-28 15:04:33.029 DeviceP[230:17733] original time string was 00:05:00
请注意,我得到的本地时间与我传递给该方法的时间字符串相同。 我看起来很不一样 post 就像 this and this。 任何帮助将不胜感激。
由于您的时间字符串在 ART 中,因此您应该在从字符串生成日期之前设置日期格式化程序的时区。表示喜欢关注
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = @"HH:mm:ss";
}
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:@"ART"];
[df setTimeZone: sourceZone];
NSDate *ds = [df dateFromString:sourceTime];
NSLog(@"sourceZone time is %@" , [df stringFromDate: ds]);
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(@"local time is %@" , [df stringFromDate: ds]);
return [df stringFromDate: d];
}