Ios 如何将英国时区转换为墨西哥时区
我有一个数组在那个日期是在英国时区。我想将英国时区转换为低于国家/地区的时间。我有两个单选按钮Ios 如何将英国时区转换为墨西哥时区,ios,objective-c,nsarray,nsdate,nstimezone,Ios,Objective C,Nsarray,Nsdate,Nstimezone,我有一个数组在那个日期是在英国时区。我想将英国时区转换为低于国家/地区的时间。我有两个单选按钮 Mexico (UTC-6) India - IST (UTC+5.30) 当我单击墨西哥(UTC-6)单选按钮时,我的阵列日期转换为墨西哥时区;当我单击印度-IST(UTC+5.30)单选按钮时,我的阵列日期转换为印度时区 我的数组[0]时区日期是2015-04-17 10:29:22+0000这是英国时区。 请帮我编码,这是我第一次做时区处理 试试看,这可能会帮上大忙。。。在这里,您需要传递时区
Mexico (UTC-6)
India - IST (UTC+5.30)
当我单击墨西哥(UTC-6)
单选按钮时,我的阵列日期转换为墨西哥时区;当我单击印度-IST(UTC+5.30)
单选按钮时,我的阵列日期转换为印度时区
我的数组[0]时区日期是2015-04-17 10:29:22+0000
这是英国时区。
请帮我编码,这是我第一次做时区处理 试试看,这可能会帮上大忙。。。在这里,您需要传递时区的名称
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Europe/London"];
NSTimeZone* destinationTimeZone = [NSTimeZone timeZoneWithName:@"America/Mexico_City"];
NSDate *yourDate = [NSDate date]; // Please add here your date that you want change .
//calc time difference
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:yourDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:yourDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//set current real date
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:yourDate];
所有时区列表在这里
请尝试此帮助…
NSDate
没有时区,因此您的NSDate
不在英国时区。NSDate
是绝对时间参考。这意味着,如果你调用[NSDate]
,而世界另一端的某个人恰好在同一时间执行此操作,那么你俩都会得到相同的结果
时区仅在显示日期时存在,但它们不是NSDate
的一部分
如果要在墨西哥时区显示日期,可以执行以下操作:
NSDate *date = // your NSDate here
NSTimeZone *myZone = [NSTimeZone timeZoneWithName:@"America/Mexico_City"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeZone:myZone];
NSString *dateString = [dateFormatter stringFromDate:date];
如果要获取其他时区的日期,只需将第一行更改为使用其他时区即可。例如,在印度,你可以使用
NSTimeZone *myZone = [NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
NSDate
没有时区,无论您如何创建它。