0
func setTimeLeft() { let timeNow = NSDate() {

    if timeEnd.compare(timeNow as Date) == ComparisonResult.orderedDescending {
        let calendar = NSCalendar.current
        let components = calendar.components([.day , .hour , .minute , .second]  , fromDate: timeNow, toDate: timeEnd, options: [])

        var dayText = String(components.day) + "d "
        var hourText = String(components.hour) + "h "

        // Hide day and hour if they are zero
        if components.day <= 0 {
            dayText = ""
            if components.hour <= 0 {
                hourText = ""
            }
        }
        timeLeftLabel.text = dayText + hourText + String(components.minute) + "m " + String(components.second) + "s"

    } else {
        timeLeftLabel.text = "Ended"
    }
}

i'm newbie and i just stuck at this error error at line

let components = calendar.components([.day , .hour , .minute , .second]  , fromDate: timeNow, toDate: timeEnd, options: [])
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Bilal
  • 11
  • 6

1 Answers1

0

Use Calendar instead of NSCalendar because NSCalendar.current will also give Calendar instance and then get DateComponents from calendar using dateComponents(_:from:to:) method.

let calendar = Calendar.current
let components = calendar.dateComponents([.day , .hour , .minute , .second], from: timeNow, to: timeEnd)
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • @Bilal Have you replace your code with my one, What is the error?, Can you show the screen shot if possible. Note that there is no option parameter in this method. – Nirav D Feb 15 '17 at 10:14
  • @Bilal Welcome mate :) – Nirav D Feb 15 '17 at 10:17
  • Nirav do will still use NSDate() in swift 3.0 or we use Date()? – Bilal Feb 15 '17 at 10:24
  • @Bilal You can use `NSDate` but it is batter if you use native `Date` because all swift's `Foundation` function need `Date` type as argument not `NSDate`. – Nirav D Feb 15 '17 at 10:25
  • can u help me again i want to give one standard time to timeEnd right now i'm doing this let timeEnd = Date(timeInterval: 7 * 60 * 60, since: Date() as Date) so every time i open this view controller it start from 0 i want something like let timeEnd = Date(timeInterval: 28 Feb 2017 , since: Date() as Date) that it end on a specific date like i mentioned above – Bilal Feb 22 '17 at 11:47
  • @Bilal Don't get you, can you add new question here with specific detail and give me the link of it here, That would be easy to understand your issue. – Nirav D Feb 22 '17 at 11:49
  • i'm asking about this same question, at the very first line i'm using timeEnd variable and comparing it with timeNow , i declared timeEnd above in code as let timeEnd = Date(timeInterval: 7 * 60 * 60, since: Date() as Date) but i want something like this let timeEnd = Date(timeInterval: 28 feb 2017 , since: Date() as Date) i want to end time on specific/mentioned/given date because right count down began everytime i switched to the viewcontroller where defined this time limit – Bilal Feb 22 '17 at 11:59
  • @Bilal You want to create timeEnd to be date of 28 feb 2017 from string? – Nirav D Feb 22 '17 at 12:02
  • i want that time end keep running till the date that i provided/given time it to be , right now it only run when i switched to view controller where i mentioned this code – Bilal Feb 22 '17 at 12:04
  • @Bilal You want to change the label's value continuously, right?\ – Nirav D Feb 22 '17 at 12:06
  • yes ,any date ...that i wrote that ending date and when date finished or passed then label shows "time ended" – Bilal Feb 22 '17 at 12:07
  • like if i give 28 feb 2017 as end date then at 12:00 am of 1 march 2017 label shows "time ended" – Bilal Feb 22 '17 at 12:09
  • @Bilal Then you need to use timer like this way http://stackoverflow.com/questions/29374553/how-to-make-a-countdown-with-nstimer-on-swift. and call your function with timer. – Nirav D Feb 22 '17 at 12:09
  • no , i want something like let timeEnd = Date( 26 feb 2017)//ending time, let timeNow = Date() // current time, if timeEnd.compare(timeNow as Date) == ComparisonResult.orderedDescending , i want to end this timer on 26 feb from current date either i run my app or not but timer kept runing and if i run my app on 27 feb it show "time ended" – Bilal Feb 22 '17 at 12:56
  • @Bilal Really don't get you. – Nirav D Feb 22 '17 at 12:57
  • @Bilal Welcome mate:) – Nirav D Feb 22 '17 at 13:00