我需要显示具体时区的日期,包括 DST(欧洲时间)。应用程序将在立陶宛使用,因此夏季时区为+3,其他时间为+2。问题是,我只有一个日期列表,我不知道如何显示夏季日期的 +3 和其他日期的 +2。目前,我有时区:
// Eastern European Summer Time UTC + 3 hours
NSTimeZone *timeZoneWithDst = [NSTimeZone timeZoneWithAbbreviation:@"EEST"];
//Eastern European Time UTC + 2 hours
NSTimeZone *timeZoneWithoutDst = [NSTimeZone timeZoneWithAbbreviation:@"EET"];
但是如何遍历我的日期列表并计算我应该向日期添加 +3 还是 +2?
更新 最后,我通过应用 Martin R. 建议按名称而不是缩写使用时区来实现它。这样,具有该时区的日期将自动处理 DST。这是我转换日期的代码:
NSTimeZone *TimeZone = [NSTimeZone timeZoneWithName:@"Europe/Vilnius"];
NSInteger seconds = [myTimeZone secondsFromGMTForDate:someDate];
NSDate *result = [NSDate dateWithTimeInterval:seconds sinceDate:someDate];
最佳答案
要将 NSDate
转换为字符串表示形式,请使用 NSDateFormatter
。默认情况下,它使用本地时区。要根据具体时区显示日期,您可以设置
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"Europe/Vilnius"];
[dateFormatter setTimeZone:tz];
(根据 http://www.timezoneconverter.com/cgi-bin/findzone ,立陶宛的时区是“欧洲/维尔纽斯”。)
关于ios - 如何显示日期和日期的时区,与定义的国家/地区相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17568870/