如何在Swift中计算具体日期(使用NSDate)

时间:2016-09-14 21:49:13

标签: swift nsdate

我正在使用try中的ios应用程序,我有一个包含7个选项的滑块。每个选项代表特定日期。

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

swift

如上所示 - 我在控制台中打印出要返回的内容。 但是,我希望以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和计算日期?

2 个答案:

答案 0 :(得分: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)
}

答案 1 :(得分:0)

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