NSCalendar得到日差错 [英] NSCalendar getting day difference wrong

查看:145
本文介绍了NSCalendar得到日差错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个不同的日期使用 [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:date] 。但是,如果我的时区不同(例如,在德国的某个地方),即使日期显然是相同的,返回的日子也不同。如果在相同的日期使用 NSYearCalendarUnit 而不是 NSEraCalendarUnit ,返回的值是相同的。

I'm trying to use [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:date] on two different dates to see if they fall on the same day. However, if my time zone is different (say, somewhere in Germany), even though the dates are obviously the same, the days returned are different. If I use NSYearCalendarUnit instead of NSEraCalendarUnit on the same dates, the returned values are the same.

使用 NSYearCalendarUnit 的唯一问题是它为不同年份的同一天返回相同的值,并不容易确定

The only problem with using NSYearCalendarUnit is it returns the same value for the same day of different years, and it's not simple to determine the number of days between two dates if they fall on different years.

任何想法出现了什么问题,或者如何更容易确定两个日期是否在同一天,或者确定

Any ideas what's wrong or how to more easily determine if two dates are on the same day, or to determine the number of days between them?

例如:

[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Berlin"]];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *date1 = [NSDate dateWithTimeIntervalSinceReferenceDate:300751200];
NSDate *date2 = [NSDate dateWithTimeIntervalSinceReferenceDate:300836062.388569];

NSLog(@"\nDate 1: %@\nDate 2: %@",date1,date2);
/*Output:
Date 1: 2010-07-14 00:00:00 +0200
Date 2: 2010-07-14 23:34:22 +0200
*/

int day1 = [cal ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit
                         forDate:date1];
int day2 = [cal ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit
                         forDate:date2];

NSLog(@"\nDay 1: %i\nDay 2: %i",day1,day2);
/*Output:
  Day 1: 733966
  Day 2: 733967
*/

day1 = [cal ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit
                     forDate:date1];
day2 = [cal ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit
                     forDate:date2];

NSLog(@"\nDay 1:%i\nDay 2: %i",day1,day2);
/*Output:
  Day 1: 195
  Day 2: 195
*/


推荐答案

请参阅 http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html
讨论

See http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html: "Discussion

序数在大多数情况下与单位的分解值不相同。通常返回值为1和更大。例如,时间00:45在一天的第一个小时,对于单位小时和天,结果将为1.例外是月份周计算,在第一天之前的天数返回0

The ordinality is in most cases not the same as the decomposed value of the unit. Typically return values are 1 and greater. For example, the time 00:45 is in the first hour of the day, and for units Hour and Day respectively, the result would be 1. An exception is the week-in-month calculation, which returns 0 for days before the first week in the month containing the date."

换句话说:23:34:22属于一天的最后一个小时,到达 24 <罢工> 0。运行时间超过零等于:第二天。

In other words: 23:34:22 belongs to the last hour of day, coming up to 240. Time running over the zero is equivalent to: next day.

也许这可以被当作一个bug:一天中的时间从第一个到第二个到第 zeroth ?我建议:填写错误报告。

Maybe this could be treated as a bug: hours in a day running from the first over the twentytird to the zeroth? I suggest: fill in a bug report.

这篇关于NSCalendar得到日差错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆