我试图获取当前日期NSDate()与PHP time();调用中的日期之间的差异,例如:NSDate(timeIntervalSinceReferenceDate: 1417147270)。如何获得两个日期之间的时间差。我想有一个比较两个日期的函数,if(seconds > 60)然后返回分钟,if(minutes > 60)返回小时和if(hours > 24)返回天等等。
NSDate()
time();
NSDate(timeIntervalSinceReferenceDate: 1417147270)
if(seconds > 60)
if(minutes > 60)
if(hours > 24)
我应该怎么做?
编辑:当前接受的答案已经完全做了我想要做的。我建议您以方便使用的方式获取它,以便以PHP time()函数使用的形式获取两个日期之间的时间。如果您对PHP不太熟悉,那么这是从1970年1月1日开始的秒数。这对于PHP后端非常有用。如果您使用的是NodeJS之类的后端,则可能需要考虑下面提供的其他一些选项。
time()
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"