user3766930 user3766930 - 2 months ago 12
Swift Question

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?

Answer

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