如何将timezone1的日期转换为设备本地时区2的日期?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Jerusalem"]];
NSDate *date = [dateFormat dateFromString:timestamp];
then something like:
[dateFormat2 setTimeZone:[NSTimeZone localTimeZone]];
date2 = [dateFormat2 dateFromDate:date withOtherTimeZone:zone];
更新
我想我明白了。
//get source date from the server
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *sourceDate = [dateFormat dateFromString:timestamp];
//set timezones
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Jerusalem"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
//calc time difference
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//set current real date
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
可以吗?
答案 0 :(得分:8)
首先关闭,时间就是时间。在这第二个,地球上的任何地方,它都是同一秒。不要再考虑时间格式了。不是。所有时间都应该是GMT,然后你可以根据TimeZone显示它。
Date *d = [NSDate date] ;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss Z"];
NSTimeZone *nyTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"sometimeabbr."];
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimezone: nyTimeZone];
NSLog(@"ny time is %@" , [df stringFromDate: d]);
[df setTimeZone: localTimeZone];
NSLog(@"local time is %@" , [df stringFromDate: d]);
如果给定一个字符串日期并需要转换,请创建NSDateFormatter以摄取字符串日期。确保设置时区。现在你有一个NSDate对象(NSDates中没有时区),你可以格式化成你想要的任何其他时区。
请为所有神圣的爱而感恩。将时间视为前进的秒数,将时区视为这些时区的格式。
答案 1 :(得分:1)
这就是答案:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Asia/Jerusalem"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
//calc time difference
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//set current real date
NSDate* date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
答案 2 :(得分:1)
*不同的时区日期在swift中转换为本地时区日期。
let timeZone1 = NSTimeZone(name: timeZone1Name)
let localTimeZone = NSTimeZone.localTimeZone()
let timeZone1Interval = timeZone1?.secondsFromGMTForDate(timezone1Date!)
let deviceTimeZoneInterval = localTimeZone.secondsFromGMTForDate(timezone1Date!)
let timeInterval = Double(timeZone1Interval! - deviceTimeZoneInterval)
let originalDate = NSDate(timeInterval: timeInterval, sinceDate: timezone1Date!)
let dateFormater : NSDateFormatter = NSDateFormatter()
dateFormater.dateFormat = "YYYY-MM-dd HH:mm:ss Z"
NSLog("Converted date: \(dateFormater.stringFromDate(originalDate))")
let dateString = dateFormater.stringFromDate(originalDate)
let localTimeZoneDate = dateFormater.dateFromString(dateString)
*