0

我习惯于setDefaultTimezone设置时区并在UITableview.在此处输入图像描述

我使用下面的代码来设置默认时区

[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", evnt.location]];
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithName:evnt.location]];
[cell.textLabel setText:[NSString stringWithFormat:@"%@",[NSDate date]]];

我必须做什么?

4

2 回答 2

1

使用 NSCalendar 和 setTimeZone 方法。

  NSDate *newDate;
  NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:~              NSTimeZoneCalendarUnit fromDate:[NSDate date]];

  newDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
  NSLog(@"newDate: %@", newDate);
  NSLog(@"newDate: %.0f", [newDate timeIntervalSinceReferenceDate]);

新日期:2011-09-09 15:02:09 +0000 新日期:337273330

  [dateComponents setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]];
  newDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
  NSLog(@"newDate: %@", newDate);
  NSLog(@"newDate: %.0f", [newDate timeIntervalSinceReferenceDate]);

新日期:2011 年 9 月 9 日 00:52:03 +0000 新时间间隔:337222930

 [dateComponents setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]];
 newDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
 NSLog(@"newDate: %@", newDate);
 NSLog(@"newDate: %.0f", [newDate timeIntervalSinceReferenceDate]);

新日期:2011-09-09 08:52:03 +0000 新时间间隔:337251730

于 2013-08-14T06:26:47.350 回答
0

-[NSDate description]始终以 UTC 格式打印日期。

所以尝试这样做:

NSDate *myDate = [[NSDate date] descriptionWithLocale:[NSLocale systemLocale]];

另外检查以下内容:

NSDate 没有返回我的本地时区/设备的默认时区

https://stackoverflow.com/questions/13260787/setdefaulttimezone-not-working

将 UTC NSDate 转换为本地时区 Objective-C

于 2013-08-14T06:36:36.110 回答