获取特定时区的两个NSDate日期之间的天数 [英] Get number of days between two NSDate dates in a particular timezone

查看:166
本文介绍了获取特定时区的两个NSDate日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现代码计算两个日期之间的差异这里

我写一个方法:

I found the codes to calculate days difference between two dates here.
I write a method :

-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger startDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit
                                            inUnit: NSEraCalendarUnit forDate:startDate];
    NSInteger endDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit
                                          inUnit: NSEraCalendarUnit forDate:endDate];
    return endDay-startDay;
}

此方法有一个问题:它不能考虑时区的事情。即使我添加了这样一行:

This method has a problem: it can't consider the timezone thing. Even I add a line like this:

[gregorian setTimeZone:[NSTimeZone localTimeZone]];

我的测试代码是这样的:

My test code is like this:

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = @"2012-09-03 23:00:00";
    NSDate *dateStart = [dateFormat dateFromString:strDate];
    strDate = @"2012-09-04 01:00:00";
    NSDate *dateEnd = [dateFormat dateFromString:strDate];
    NSLog(@"Days difference between %@ and %@  is:  %d days",[dateFormat stringFromDate:dateStart],[dateFormat stringFromDate:dateEnd],[self daysWithinEraFromDate:dateStart toDate:dateEnd]);

结果是:


2012-09-03 23:00:00和2012-09-04 01:00:00之间的差异是:0天

Days difference between 2012-09-03 23:00:00 and 2012-09-04 01:00:00 is: 0 days

我希望通过两个日期之间的午间数量获得1天的结果。我的时区是GMT +8。但这个计算是基于GMT,所以我收到错误的天数。有没有办法解决这个问题?谢谢。

I want to get 1 day as result by the number of midnights between the two dates. My timezone is GMT +8. But this calculation is based on GMT, so I get the wrong days number. Is there anyway to solve this problem? Thank you.

Scott Lemmon 的方法可以解决我的问题。我重写了这样的代码:

Scott Lemmon's method can solve my problem. I rewrite my code like this:

-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    [gregorian setTimeZone:[NSTimeZone localTimeZone]];
    NSDate *newDate1 = [startDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
    NSDate *newDate2 = [endDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];

    NSInteger startDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit
                                            inUnit: NSEraCalendarUnit forDate:newDate1];
    NSInteger endDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit
                                          inUnit: NSEraCalendarUnit forDate:newDate2];
    return endDay-startDay;
}


推荐答案

如果时区偏移isn不用工作,只是手动添加或减少呢?

If the time zone offset isn't working, how about just add or subtract it manually instead?

在你的情况下 NSDate * newDate = [oldDate dateByAddingTimeInterval:( - 8 * 60 * 60)]; 减去你的+8小时。

In your case NSDate *newDate = [oldDate dateByAddingTimeInterval:(-8 * 60 * 60)]; to subtract off your +8 hours.

或者如果你想自动找到GMT偏移,那么它只是 NSDate * newDate = [ oldDate dateByAddingTimeInterval:( - [[NSTimeZone localTimeZone] secondsFromGMT])

Or if you want to find the GMT offset automatically as well then it would simply be NSDate *newDate = [oldDate dateByAddingTimeInterval:(-[[NSTimeZone localTimeZone] secondsFromGMT])

另一个想法:
一个更容易的解决方案是完全忽视时间信息。只要将日期设置为相同的任意数字,那么只要日期来自相同的时区,您将永远得到它们之间的正确数字,无论GMT偏移如何。

Another thought: A perhaps easier solution would be to just disregard the time information altogether. Just set it to the same arbitrary number for both dates, then as long as the dates come from the same timezone you will always get the correct number of mid-nights between them, regardless of GMT offset.

这篇关于获取特定时区的两个NSDate日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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