【问题标题】:how can I calculate specific dates in Swift (using NSDate)如何在 Swift 中计算特定日期(使用 NSDate)
【发布时间】:2016-09-14 21:49:12
【问题描述】:

我正在swift 开发 ios 应用程序,我有一个带有 7 个选项的滑块。每个选项代表特定日期。

到目前为止,我的代码如下:

func convertValueToDate(value: Float) -> NSDate{

    var currentDate:NSDate = NSDate()

    switch(value){
    case 1:
        print("5 years ago")
        return NSDate()
    case 2:
        print("one year ago")
        return NSDate()
    case 3:
        print("six months ago")
        return NSDate()
    case 4:
        print("one month ago")
        return NSDate()
    case 5:
        print("one week ago")
        return NSDate()
    case 6:
        print("yesterday")
        return NSDate()
    case 7:
        print("today")
        return NSDate()
    default:
        print("default date")
        return NSDate()
    }
}

如您在上面看到的 - 我在控制台中打印我想要返回的内容。 但我不想打印,而是想以NSDate 格式返回这些日期。 我不知道如何计算每个选项的时间,因为我希望每天都有0:00:01 AM 的时间。所以例如。当用户输入号码7 时,我想将昨天0:00:01 AM 的确切日期返回给他。当用户选择号码5时,我想给他一周前的日期和时间0:00:01 AM,依此类推。我如何计算它,所以这个函数总是返回时间0:00:01 AM 和计算日期?

【问题讨论】:

    标签: swift nsdate


    【解决方案1】:

    您可以使用 NSCalendar 方法 dateByAddingUnit:

    func convertValueToDate(value: Float) -> NSDate {
        struct Cal {
            static let iso8601 = NSCalendar(identifier: NSCalendarIdentifierISO8601)!
        }
        let now = NSDate()
        print("now: ", now)
        switch(value) {
        case 1:
            print("5 years ago")
            return Cal.iso8601.dateByAddingUnit(.Year, value: -5, toDate: now, options: [])!
        case 2:
            print("one year ago")
            return Cal.iso8601.dateByAddingUnit(.Year, value: -1, toDate: now, options: [])!
        case 3:
            print("six months ago")
            return Cal.iso8601.dateByAddingUnit(.Month, value: -6, toDate: now, options: [])!
        case 4:
            print("one month ago")
            return Cal.iso8601.dateByAddingUnit(.Month, value: -1, toDate: now, options: [])!
        case 5:
            print("one week ago")
            return Cal.iso8601.dateByAddingUnit(.WeekOfYear, value: -1, toDate: now, options: [])!
        case 6:
            print("yesterday")
            return Cal.iso8601.dateByAddingUnit(.Day, value: -1, toDate: now, options: [])!
        case 7:
            print("today")
            return now
        default:
            print("default date")
            return now
        }
    }
    

    如果您需要返回该日期的开始日期,您可以使用 NSCalendar startOfDayForDate 方法。

    func convertValueToDate(value: Float) -> NSDate {
        struct Cal {
            static let iso8601 = NSCalendar(identifier: NSCalendarIdentifierISO8601)!
        }
        let now = NSDate()
        print("now: ", now)
        let result: NSDate
        switch(value) {
        case 1:
            print("5 years ago")
            result = Cal.iso8601.dateByAddingUnit(.Year, value: -5, toDate: now, options: [])!
        case 2:
            print("one year ago")
            result =  Cal.iso8601.dateByAddingUnit(.Year, value: -1, toDate: now, options: [])!
        case 3:
            print("six months ago")
            result =  Cal.iso8601.dateByAddingUnit(.Month, value: -6, toDate: now, options: [])!
        case 4:
            print("one month ago")
            result =  Cal.iso8601.dateByAddingUnit(.Month, value: -1, toDate: now, options: [])!
        case 5:
            print("one week ago")
            result =  Cal.iso8601.dateByAddingUnit(.WeekOfYear, value: -1, toDate: now, options: [])!
        case 6:
            print("yesterday")
            result =  Cal.iso8601.dateByAddingUnit(.Day, value: -1, toDate: now, options: [])!
        case 7:
            print("today")
            result = now
        default:
            print("default date")
            result = now
        }
        return Cal.iso8601.startOfDayForDate(result)
    }
    

    【讨论】:

    • 感谢您的回答!那么startOfDayForDate 方法实际上给我带来了给定日期的0:00:01AM 吗?我这样做是为了测试:print(now) print(Cal.iso8601.startOfDayForDate(now)) 并打印了两个值:2016-09-14 22:34:07 +00002016-09-14 22:00:00 +0000 为什么第二个值是 22:00:00 而不是 0:00:01
    • 为什么需要 1 秒?日期正确,不用担心日期打印UTC时间
    • 我认为 1 秒是必要的,因为我需要一天的开始,0:00:00 属于前一天,0:00:01 属于当前一天
    • 你不需要添加那一秒
    【解决方案2】:

    这里是 Swift 3 中的一个解决方案,因为它已经在这里了(对于 Swift 2.x,在类之前添加 NS 前缀应该可以解决问题):

    import Foundation
    
    func convertValueToDate(value: Float) -> Date{
    
        let calendar = Calendar.current
        let currentDate = calendar.date(bySettingHour: 00, minute: 00, second: 01, of: Date())!
    
        func dateByAdding(_ value: Int, _ component: Calendar.Component) -> Date {
            return calendar.date(byAdding: component, value: value, to: currentDate)!
        }
    
        switch(value){
        case 1:
            print("5 years ago")
            return dateByAdding(-5, .year)
        case 2:
            print("one year ago")
            return dateByAdding(-1, .year)
        case 3:
            print("six months ago")
            return dateByAdding(-6, .month)
        case 4:
            print("one month ago")
            return dateByAdding(-1, .month)
        case 5:
            print("one week ago")
            return dateByAdding(-7, .day)
        case 6:
            print("yesterday")
            return dateByAdding(-1, .day)
        case 7:
            print("today")
            return currentDate
        default:
            print("default date")
            return currentDate
        }
    }
    

    【讨论】:

    • 不是每个日期都有凌晨 12 点,所以这可能会导致您的应用崩溃
    猜你喜欢
    • 2015-06-25
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多