简体   繁体   中英

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

I'm working on ios app in swift and I have a slider with 7 options. Each option represents specific date.

So far my code is as follows:

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()
    }
}

as you can see above - I print in the console what I want to return. But instead of printing I want to return those dates in a NSDate format. I don't know how to calculate the time of each option, because I want to have the hour of 0:00:01 AM each day. So eg. when user puts number 7 I want to return him the exact date of yesterday's 0:00:01 AM . When user selects number 5 I want to give him the date one week ago with the time 0:00:01 AM , and so on. How can I calculate it so this function always returns me the time 0:00:01 AM and calculated date?

You can use NSCalendar method 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
    }
}

if you need to return the start of the day for that date you can use NSCalendar startOfDayForDate method.

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

Here a solution in Swift 3 since it is already here (For Swift 2.x adding a NS prefix before the classes should do the trick) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM