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?