在 Swift 中获取两个日期(月/日/小时/分钟/秒)之间的差异 [英] Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift

查看:31
本文介绍了在 Swift 中获取两个日期(月/日/小时/分钟/秒)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取当前日期作为 NSDate() 和来自 PHP time(); 调用的日期之间的差异,例如:NSDate(timeIntervalSinceReferenceDate: 1417147270).我如何获得两个日期之间的时间差.我想要一个函数来比较两个日期和 if(seconds > 60) 然后它返回分钟,if(minutes > 60) 返回小时和if(hours > 24) 返回天数等等.

I am trying to get the difference between the current date as NSDate() and a date from a PHP time(); call for example: NSDate(timeIntervalSinceReferenceDate: 1417147270). How do I go about getting the difference in time between the two dates. I'd like to have a function that compares the two dates and if(seconds > 60) then it returns minutes, if(minutes > 60) return hours and if(hours > 24) return days and so on.

我应该怎么做?

当前接受的答案正是我想做的.我推荐它以方便使用以 PHP time() 函数使用的形式获取两个日期之间的时间.如果您对 PHP 不是特别熟悉,那是从 1970 年 1 月 1 日开始的以秒为单位的时间.这对 PHP 后端很有用.如果您正在使用像 NodeJS 这样的后端,您可能需要考虑下面提供的一些其他选项.

The current accepted answer has done exactly what I've wanted to do. I recommend it for easy usage for getting the time between two dates in the form that that PHP time() function uses. If you aren't particularly familiar with PHP, that's the time in seconds from January 1st, 1970. This is beneficial for a backend in PHP. If perhaps you're using a backend like NodeJS you might want to consider some of the other options you'll find below.

推荐答案

Xcode 8.3 • Swift 3.1 或更高版本

您可以使用日历来帮助您创建扩展程序来进行日期计算,如下所示:

You can use Calendar to help you create an extension to do your date calculations as follow:

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 ""
    }
}


使用日期组件格式化程序


Using Date Components Formatter

let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [.second, .minute, .hour, .day, .weekOfMonth, .month, .year]
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"

这篇关于在 Swift 中获取两个日期(月/日/小时/分钟/秒)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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