如何计算 Swift 中的特定日期(使用 NSDate)

how can I calculate specific dates in Swift (using NSDate)

我正在 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()
    }
}

正如您在上面看到的那样 - 我在控制台中打印了我想要的内容 return。 但是我不想打印 return 这些日期 NSDate 格式。 我不知道如何计算每个选项的时间,因为我想每天有0:00:01 AM小时。所以例如。当用户输入数字 7 时,我想 return 他告诉他昨天 0:00:01 AM 的确切日期。当用户选择数字 5 时,我想给他一周前的日期和时间 0:00:01 AM,依此类推。我如何计算它,以便此函数始终 return 向我发送时间 0:00:01 AM 和计算日期?

您可以使用 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
    }
}

如果您需要 return 该日期的开始日期,您可以使用 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)
}

这里是 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
    }
}