NSDate未返回设备的本地时区/默认时区

时间:2010-12-28 16:06:14

标签: iphone ios

我的本​​地和默认时区是GMT +5,但是当我通过NSDate获取日期和时间时,它会返回GMT日期和时间。

例如,在设备上测试时我的代码的代码和输出为,[设备时区Islamabad GMT +5]

 NSTimeZone *lo = [NSTimeZone localTimeZone];
 NSLog(@" - current  local timezone  is  %@",lo); // GMT +5

2010-12-28 20:56:11.785 Done[484:307]  - current  local timezone  is  Local Time Zone (Asia/Karachi (GMT+05:00) offset 18000)

 NSTimeZone *df = [NSTimeZone defaultTimeZone];
 NSLog(@" - current  default timezone  is  %@",df); // GMT +5

2010-12-28 20:56:11.790 Done[484:307]  - current  default timezone  is  Asia/Karachi (GMT+05:00) offset 18000

但是

 NSDate *cDate = [NSDate date];
 NSLog(@"current date by NSDate %@",cDate); //but NSDate show GMT
2010-12-28 20:56:11.794 Done[484:307] current date by NSDate 2010-12-28 15:56:11 GMT


 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
 //// NSTimeZone *gmt = [NSTimeZone ]
 NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT+05:00"];
 [dateFormatter setTimeZone:gmt];

 NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
 NSLog(@" date string object  %@" ,timeStamp);   // string From Date is GMT +5
2010-12-28 20:56:11.802 Done[484:307]  date string object  2010-12-28T20:56


 NSDate *datef = [dateFormatter dateFromString:timeStamp]; 
 NSLog(@" date object %@" ,datef);  // the date form above string gives again GMT
2010-12-28 20:56:11.809 Done[484:307]  **date object 2010-12-28 15:56:00 GMT**

为什么NSDate没有给当地时间?请帮忙......

7 个答案:

答案 0 :(得分:71)

试试这个......

NSDate* sourceDate = [NSDate date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];

此帮助响应当前系统时区。

答案 1 :(得分:23)

忘记NSDateFormatter这太复杂了,而是试试这个魔法:

NSLog(@"date with local timezone is: %@",
[date descriptionWithLocale:[NSLocale systemLocale]]);

答案 2 :(得分:8)

NSDate是一个“原始”日期。这就是GMT的原因。由代码来决定使用NSDateFormatter(就像你所做的那样)将日期输出到对用户有意义的值。

在极少数情况下,您可能需要显示不使用用户时区的NSDate(如果您想在纽约时间显示时间,无论用户身在何处)。然后,将日期格式化程序上的时区设置为特定值(再次,就像您所做的那样)。

计算机通常会将所有日期存储在GMT中,然后调整它们为用户显示的方式。如果你开始试图改变日期的实际存储方式,你就会搞砸很多日期处理框架,这些框架都假设你的NSDate是GMT格式。

如您所见,当您通过NSDateFormatter读取日期时,它会根据您设置的格式化程序的时区输入的时间进行转换,然后转换为GMT。那么你认为它在做什么是错的?因为它正在做它应该做的事情 - 在GMT中存储日期并根据时区输出字符串。

答案 3 :(得分:4)

试试这个

- (NSDate *) GetLocalDate {

    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    theCalendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    dayComponent = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];

    NSDate *localDate = [theCalendar dateFromComponents:dayComponent];

    return localDate;
}

- (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"localDate : %@",[self GetLocalDate]);
}

答案 4 :(得分:3)

如果您想要当地的日期和时间。试试这个代码: -

NSString *localDate = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];

答案 5 :(得分:2)

Eliseo Chavez Jr的答案翻译成Swift

let sourceDate = NSDate();
let sourceTimeZone = NSTimeZone(abbreviation: "GMT")!;
let destinationTimeZone = NSTimeZone.systemTimeZone();
let sourceGMTOffset: NSInteger  = sourceTimeZone.secondsFromGMTForDate(sourceDate);
let destinationGMTOffset:NSInteger  = destinationTimeZone.secondsFromGMTForDate(sourceDate);
let interval: NSTimeInterval = Double (destinationGMTOffset - sourceGMTOffset);
let destinationDate = NSDate(timeInterval: interval, sinceDate: sourceDate);

答案 6 :(得分:1)

这对我来说非常适合在我的时区中获取日期并按照我想要的格式进行格式化:

NSDate* gmtDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Los_Angeles"]];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:gmtDate];