两个NSDate之间的天数 [英] Number of days between two NSDates

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

问题描述

如何确定两个NSDate值之间的天数(同时考虑时间)?



NSDate值的格式为[NSDate date]



具体来说,当用户在我的iPhone应用程序中进入非活动状态时,我存储以下值:

  exitDate = [NSDate date]; 

当他们打开应用程序时,我得到当前时间:

  NSDate * now = [NSDate date]; 

现在我想实现以下操作:

   - (int)numberOfDaysBetweenStartDate:exitDate andEndDate:now 


解决方案

这里是一个实现,我用来确定两个日期之间的日历天数:

  +(NSInteger)daysBetweenDate:(NSDate *)fromDateTime andDate:(NSDate *)toDateTime 
{
NSDate * fromDate;
NSDate * toDate;

NSCalendar * calendar = [NSCalender currentCalendar];

[calendar rangeOfUnit:NSCalendarUnitDay startDate:& fromDate
interval:NULL forDate:fromDateTime];
[calendar rangeOfUnit:NSCalendarUnitDay startDate:& toDate
interval:NULL forDate:toDateTime];

NSDateComponents * difference = [calendar components:NSCalendarUnitDay
fromDate:fromDate toDate:toDate options:0];

return [difference day];
}

EDIT: b

上面的奇妙解决方案,下面的Swift版本作为 NSDate 的扩展:

  extension NSDate {
func numberOfDaysUntilDateTime(toDateTime:NSDate,inTimeZone timeZone:NSTimeZone?= nil) - > Int {
let calendar = NSCalendar.currentCalendar()
如果let timeZone = timeZone {
calendar.timeZone = timeZone
}

var fromDate:NSDate ?,toDate:NSDate?

calendar.rangeOfUnit(.Day,startDate:& fromDate,interval:nil,forDate:self)
calendar.rangeOfUnit(.Day,startDate:& toDate,interval:nil, forDate:toDateTime)

let difference = calendar.components(.Day,fromDate:fromDate !, toDate:toDate !, options:[])
return difference.day
}
}

根据您的需要,



上述解决方案也适用于当前时区以外的时区,非常适合显示世界各地信息的应用程序。


How could I determine the number of days between two NSDate values (taking into consideration time as well)?

The NSDate values are in whatever form [NSDate date] takes.

Specifically, when a user enters the inactive state in my iPhone app, I store the following value:

exitDate = [NSDate date];

And when they open the app back up, I get the current time:

NSDate *now = [NSDate date];

Now I'd like to implement the following:

-(int)numberOfDaysBetweenStartDate:exitDate andEndDate:now

解决方案

Here's an implementation I used to determine the number of calendar days between two dates:

+ (NSInteger)daysBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime
{
    NSDate *fromDate;
    NSDate *toDate;

    NSCalendar *calendar = [NSCalendar currentCalendar];

    [calendar rangeOfUnit:NSCalendarUnitDay startDate:&fromDate
        interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSCalendarUnitDay startDate:&toDate
        interval:NULL forDate:toDateTime];

    NSDateComponents *difference = [calendar components:NSCalendarUnitDay
        fromDate:fromDate toDate:toDate options:0];

    return [difference day];
}

EDIT:

Fantastic solution above, here's Swift version below as an extension on NSDate:

extension NSDate {
  func numberOfDaysUntilDateTime(toDateTime: NSDate, inTimeZone timeZone: NSTimeZone? = nil) -> Int {
    let calendar = NSCalendar.currentCalendar()
    if let timeZone = timeZone {
      calendar.timeZone = timeZone
    }

    var fromDate: NSDate?, toDate: NSDate?

    calendar.rangeOfUnit(.Day, startDate: &fromDate, interval: nil, forDate: self)
    calendar.rangeOfUnit(.Day, startDate: &toDate, interval: nil, forDate: toDateTime)

    let difference = calendar.components(.Day, fromDate: fromDate!, toDate: toDate!, options: [])
    return difference.day
  }
}

A bit of force unwrapping going on which you may want to remove depending on your use case.

The above solution also works for time zones other than the current time zone, perfect for an app that shows information about places all around the world.

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

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