简体   繁体   English

将日期从时区转换为设备本地时区和日期

[英]Convert date from timezone to device local timezone and date

How can I convert date with timezone1 to date with device local timezone2? 如何将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];

UPDATE: 更新:

I think I got it. 我想我明白了。

//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];

Is it ok? 好吗?

OK first off, time is time. 好先关,时间到了。 At this very second, everywhere on earth, its is the same second. 在这第二个,地球上的任何地方,它都是同一秒。 Stop thinking of time format as time. 不要再考虑时间格式了。 It's not. 不是。 All time should be in GMT, and then you can display it based on TimeZone. 所有时间都应该是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]);

If you are given a string date and need to convert, create NSDateFormatter to ingest the string date. 如果给定一个字符串日期并需要转换,请创建NSDateFormatter以摄取字符串日期。 Make sure you set a timezone. 确保设置时区。 Now you have a NSDate object (no timezone in NSDates), that yiou can format into any other timezone you want. 现在你有一个NSDate对象(在NSDates中没有时区),你可以格式化成你想要的任何其他时区。

Please for the love all all that is holy. 请为所有神圣的爱所有。 Treat time as seconds that marches forward, and timezones as the formatting of those timezones. 将时间视为前进的秒数,将时区视为这些时区的格式。

This is the answer: 这就是答案:

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];

*Different time zone date convert to local time zone date in swift. *不同的时区日期在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)

* *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM