ios - 如何在iOS上将日期与时区进行比较

标签 ios datetime timezone nsdate nstimezone

我在寻找一种方法来比较日期和ios上的时区。很难,因为NSDate仅使用绝对时区。从iOS的JodaTime库中找不到等效的DateTime

我想实现方法:

- (BOOL)isTheSameDayForDate:(NSDate *)date1 withTimeZone:(NSTimeZone *)timeZone1 andDate:(NSDate *)date2 withTimeZone:(NSTimeZone *)timeZone2;

从timezone2角度来看,如果date1和date2及其时区在同一天,则此方法返回true。

对于:2014-10-09T03:00:00 + 03:00和2014-10-09T23:00:00 + 03:00返回YES。

对于:2014-10-09T00:03:00 + 03:00和2014-10-10T00:03:00 + 03:00返回NO。

看起来很简单,但请注意以下几点:

2014-10-08T16:00:00-8:00和2014-10-09T1:00:00 + 02:00返回YES

2014-10-08T13:00:00-8:00和2014-10-09T1:00:00 + 02:00返回 NO

最佳答案

您应该在NSCalendar上使用为此目的设计的新方法。像这样:

NSDate *date1;
NSDate *date2;
NSTimeZone *myTimeZone; // zone of interest

// Set up dates and zone, then do this
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:myTimeZone];
BOOL sameDay = [calendar isDate:date1 inSameDayAsDate:date2];

关于ios - 如何在iOS上将日期与时区进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961774/

相关文章:

ios - 长按 UIButton 上的手势识别器?

php - 日期时间不会在数据库中更新

python - 将 unix 时间戳更改为不同的时区

java - 从时区偏移获取时区?

php - MYSQL在PHP代码中设置时区

jquery - 使用 jQuery 将时间转换为不同时区

ios - 尝试将邮政编码从本地填充的类获取到变量中,但出现了令人讨厌的循环

ios - 在自定义UIview中设置UItableview的delegate和数据源

ios - 如何将应用程序音量与系统音量(iOS设备音量物理键)分开设置?

jquery - 指定的值不符合要求的格式yyyy-MM-dd