Iphone 将GMT NSDate转换为设备';当前时区
我正在使用Parse.com存储一些值:Iphone 将GMT NSDate转换为设备';当前时区,iphone,ios,objective-c,parse-platform,Iphone,Ios,Objective C,Parse Platform,我正在使用Parse.com存储一些值: 这些是GMT值。如何将其转换为设备的当前时区并获得NSDate结果?您可以使用NSDateFormatter来实现此结果 NSString *dateAsString = @"08/07/2013 04:06"; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"dd/MM/yyyy HH:mm"]; NSTimeZone *gmt = [NSTime
这些是GMT值。如何将其转换为设备的当前时区并获得NSDate结果?您可以使用NSDateFormatter来实现此结果
NSString *dateAsString = @"08/07/2013 04:06";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy HH:mm"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[df setTimeZone:gmt];
NSDate *myDate = [df dateFromString: dateAsString];
NSLog(@"date: %@", myDate);
这个问题有很多答案,但我建议:
NSString*dateStr=@“2012-07-1607:33:01”;
NSDateFormatter*DateFormatter 1=[[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:@“yyyy-MM-dd HH:MM:ss”];
NSDate*date=[dateFormatter1 dateFromString:dateStr];
NSLog(@“日期:%@”,日期);
NSTimeZone*currentTimeZone=[NSTimeZone localTimeZone];// NSDate总是以GMT表示。只是您表示它的方式可能会改变
如果要将日期打印到label.text
,请使用NSDateFormatter
和[NSTimeZone localTimeZone]
将其转换为字符串,如下所示:
NSString *gmtDateString = @"08/12/2013 21:01";
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"dd/MM/yyyy HH:mm"];
//Create the date assuming the given string is in GMT
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [df dateFromString:gmtDateString];
//Create a date string in the local timezone
df.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [df stringFromDate:date];
NSLog(@"date = %@", localDateString);
// My local timezone is: Europe/London (GMT+01:00) offset 3600 (Daylight)
// prints out: date = 08/12/2013 22:01
我发现的最简单的方法是:
NSDate *someDateInUTC = …;
NSTimeInterval timeZoneSeconds = [[NSTimeZone localTimeZone] secondsFromGMT];
NSDate *dateInLocalTimezone = [someDateInUTC dateByAddingTimeInterval:timeZoneSeconds];
创建如下所示的Xcode测试用例可以帮助我们“永远”记住规则:
这是将NSDate更改为本地时区日期的一种非常干净的方法
extension NSDate {
func toLocalTime() -> NSDate {
let timeZone = NSTimeZone.local
let seconds : TimeInterval = Double(timeZone.secondsFromGMT(for:self as Date))
let localDate = NSDate(timeInterval: seconds, since: self as Date)
return localDate
}
}
摘自当然!但是,myDate仍将以GMT为单位,并且我希望它位于设备的当前时区。@NSMutableString,此解决方案有效,但不会将myDate区域设置/时区更改为所需的区域设置/时区。您的解决方案将日期字符串更改/格式化为GMT格式的日期。此问题已被S.O.回答了十几次。只需搜索“iOS GMT到本地”-Oscar,请确保将以下众多答案中的一个标记为已接受。这样社区就知道他们中谁解决了你的问题。@Aaron,当然!我在测试:)这是一个很好的答案,但下面我的有什么问题吗?这是5行代码,做的是相同的事情。NSDate
s根本没有时区(或小时、天等);它们只是时间点。伦敦人称之为下午5:30的NSDate
与加利福尼亚州库珀蒂诺市上午9:30的NSDate
完全相同。我理解这个概念,但我缺少的是时区forsecond格林尼治标准时间:这是真正的诀窍,谢谢:)这个概念对我有效,但我必须做到以下几点:[[NSDate alloc]initWithTimeInterval:-timeZoneSeconds sinceDate:someDateInUTC]代码>(使用PST时区测试)工作正常。我搜索了相当长的一段时间,似乎大多数人关心的是输出到字符串中,而不是执行特定于用户区域设置的日期逻辑。这种逻辑将与日期有关
- (void)test2015_05_23_07_07_07_Toronto {
NSString *utcDateString = @"2015_05_23 12:07:07";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"yyyy_MM_dd HH:mm:ss";
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDate *date2015_05_23_12_07_07_UTC = [dateFormatter dateFromString:utcDateString];
XCTAssertTrue([date2015_05_23_12_07_07_UTC.description isEqualToString:@"2015-05-23 12:07:07 +0000"]);
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"EST"];
NSString *torontoDateString = [dateFormatter stringFromDate:date2015_05_23_12_07_07_UTC];
XCTAssertTrue([torontoDateString isEqualToString:@"2015_05_23 07:07:07"]);
// change format to add ZZZ
dateFormatter.dateFormat = @"yyyy_MM_dd HH:mm:ss ZZZ";
torontoDateString = [dateFormatter stringFromDate:date2015_05_23_12_07_07_UTC];
XCTAssertTrue([torontoDateString isEqualToString:@"2015_05_23 07:07:07 -0500"]);
XCTAssertEqual(1432382827, [date2015_05_23_12_07_07_UTC timeIntervalSince1970]);
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"EST"];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date2015_05_23_12_07_07_UTC];
XCTAssertEqual(2015, components.year);
XCTAssertEqual(5, components.month);
XCTAssertEqual(23, components.day);
XCTAssertEqual(7, components.hour);
XCTAssertEqual(7, components.minute);
XCTAssertEqual(7, components.second);
}
extension NSDate {
func toLocalTime() -> NSDate {
let timeZone = NSTimeZone.local
let seconds : TimeInterval = Double(timeZone.secondsFromGMT(for:self as Date))
let localDate = NSDate(timeInterval: seconds, since: self as Date)
return localDate
}
}