0

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?

user3766930
  • 5,629
  • 10
  • 51
  • 104

2 Answers2

1

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)
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • Thanks for your answer! So does the method `startOfDayForDate` actually brings me the `0:00:01AM` of given day? I did this for test: `print(now) print(Cal.iso8601.startOfDayForDate(now))` and it printed two values: `2016-09-14 22:34:07 +0000` and `2016-09-14 22:00:00 +0000` why the second value is `22:00:00` instead of `0:00:01`? – user3766930 Sep 14 '16 at 22:36
  • Why do you need 1 second? the date is correct, don't worry about the date printing UTC time – Leo Dabus Sep 14 '16 at 22:37
  • I thought that 1 second is necessary since I need a beginning of the day and `0:00:00` belongs to the previous day and `0:00:01` to the current one – user3766930 Sep 14 '16 at 22:39
  • You don't need to add that second – Leo Dabus Sep 14 '16 at 22:40
  • Why are you using "iso8601" instead of `NSCalendar.current()`? – Qbyte Sep 14 '16 at 22:49
  • I am not displaying anything. I am using it only for date calculations – Leo Dabus Sep 14 '16 at 22:51
  • @LeoDabus can you tell me how could I return the date and time in the format `yyyy-MM-dd'T'HH:mm:ss.SSSZ`? – user3766930 Sep 16 '16 at 16:45
  • 1
    @user3766930 http://stackoverflow.com/questions/28016578/swift-how-to-create-a-date-time-stamp-and-format-as-iso-8601-rfc-3339-utc-tim/28016692#28016692 – Leo Dabus Sep 16 '16 at 22:06
  • You might need to look at the edit history for Swift 2.x syntax – Leo Dabus Sep 16 '16 at 22:07
0

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
    }
}
Qbyte
  • 12,753
  • 4
  • 41
  • 57