为了账号安全,请及时绑定邮箱和手机立即绑定

在(月/天/小时/分钟/秒)内得到两个NSDate之间的差额

在(月/天/小时/分钟/秒)内得到两个NSDate之间的差额

三国纷争 2019-06-23 16:32:15
在(月/天/小时/分钟/秒)内得到两个NSDate之间的差额我正试图找出当前日期之间的差异,因为NSDate()和PHP的日期time();例如:NSDate(timeIntervalSinceReferenceDate: 1417147270)..我怎样才能得到这两个日期之间的时间差呢?我想要一个比较这两个日期的函数if(seconds > 60)然后再回来几分钟,if(minutes > 60)返回时间和if(hours > 24)返回的日子,以此类推。我该怎么做?编辑:当前接受的答案完全符合我想做的事情。为了便于使用,我推荐它来获取PHP表单中的两个日期之间的时间。time()函数使用。如果您对PHP并不特别熟悉,那么从1970年1月1日起,这是几秒钟内的时间。这对PHP的后端是有益的。如果您正在使用像NodeJS这样的后端,您可能需要考虑下面的一些其他选项。
查看完整描述

3 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞


Xcode 8.3·Swift 3.1或更高版本


您可以使用“日历”来帮助您创建一个扩展来执行日期计算,如下所示:


extension Date {

    /// Returns the amount of years from another date

    func years(from date: Date) -> Int {

        return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0

    }

    /// Returns the amount of months from another date

    func months(from date: Date) -> Int {

        return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0

    }

    /// Returns the amount of weeks from another date

    func weeks(from date: Date) -> Int {

        return Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0

    }

    /// Returns the amount of days from another date

    func days(from date: Date) -> Int {

        return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0

    }

    /// Returns the amount of hours from another date

    func hours(from date: Date) -> Int {

        return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0

    }

    /// Returns the amount of minutes from another date

    func minutes(from date: Date) -> Int {

        return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0

    }

    /// Returns the amount of seconds from another date

    func seconds(from date: Date) -> Int {

        return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0

    }

    /// Returns the a custom time interval description from another date

    func offset(from date: Date) -> String {

        if years(from: date)   > 0 { return "\(years(from: date))y"   }

        if months(from: date)  > 0 { return "\(months(from: date))M"  }

        if weeks(from: date)   > 0 { return "\(weeks(from: date))w"   }

        if days(from: date)    > 0 { return "\(days(from: date))d"    }

        if hours(from: date)   > 0 { return "\(hours(from: date))h"   }

        if minutes(from: date) > 0 { return "\(minutes(from: date))m" }

        if seconds(from: date) > 0 { return "\(seconds(from: date))s" }

        return ""

    }

}

使用日期组件格式化程序


let dateComponentsFormatter = DateComponentsFormatter()

dateComponentsFormatter.allowedUnits = [.year,.month,.weekOfMonth,.day,.hour,.minute,.second]

dateComponentsFormatter.maximumUnitCount = 1

dateComponentsFormatter.unitsStyle = .full

dateComponentsFormatter.string(from: Date(), to: Date(timeIntervalSinceNow: 4000000))  // "1 month"

let date1 = DateComponents(calendar: .current, year: 2014, month: 11, day: 28, hour: 5, minute: 9).date!

let date2 = DateComponents(calendar: .current, year: 2015, month: 8, day: 28, hour: 5, minute: 9).date!


let years = date2.years(from: date1)     // 0

let months = date2.months(from: date1)   // 9

let weeks = date2.weeks(from: date1)     // 39

let days = date2.days(from: date1)       // 273

let hours = date2.hours(from: date1)     // 6,553

let minutes = date2.minutes(from: date1) // 393,180

let seconds = date2.seconds(from: date1) // 23,590,800


let timeOffset = date2.offset(from: date1) // "9M"


let date3 = DateComponents(calendar: .current, year: 2014, month: 11, day: 28, hour: 5, minute: 9).date!

let date4 = DateComponents(calendar: .current, year: 2015, month: 11, day: 28, hour: 5, minute: 9).date!


let timeOffset2 = date4.offset(from: date3) // "1y"


let date5 = DateComponents(calendar: .current, year: 2017, month: 4, day: 28).date!

let now = Date()

let timeOffset3 = now.offset(from: date5) // "1w"


查看完整回答
反对 回复 2019-06-23
  • 3 回答
  • 0 关注
  • 602 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信