Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift countdown timer- displays days hours seconds remaining

Hello I am very new to swift and I was trying to create an app that counts down to an event on a specific date. I want it to show the number of days hours and seconds left until the specified date but I cannot figure out how to do this.

Please help!

like image 653
jss0199 Avatar asked Feb 09 '16 05:02

jss0199


People also ask

How do you create a 60s countdown timer that prints out the seconds remaining every second in Swift?

Underneath the timerLabel outlet create the following variables: var seconds = 60 //This variable will hold a starting value of seconds. It could be any amount above 0. var timer = Timer() var isTimerRunning = false //This will be used to make sure only one timer is created at a time.

How do I display a timer in swift 5?

let timer1 = Timer. scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: false) let timer2 = Timer. scheduledTimer(withTimeInterval: 1.0, repeats: false) { timer in print("Timer fired!") }

How do timers work in Swift?

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A nonrepeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate() method.

How do you call a timer in Swift?

let timer = Timer. scheduledTimer(withTimeInterval: 2.0, repeats: true) { timer in print("timer executed...") } Above, we have scheduled a repeating timer using a closure. In this way, you will receive the timer's execution within the same block.


3 Answers

Swift 4

var releaseDate: NSDate?
var countdownTimer = Timer()

func startTimer() {

    let releaseDateString = "2018-09-16 08:00:00"
    let releaseDateFormatter = DateFormatter()
    releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    releaseDate = releaseDateFormatter.date(from: releaseDateString)! as NSDate

    countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
}

@objc func updateTime() {

    let currentDate = Date()
    let calendar = Calendar.current

    let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate, to: releaseDate! as Date)

    let countdown = "Days \(diffDateComponents.day ?? 0), Hours \(diffDateComponents.hour ?? 0), Minutes \(diffDateComponents.minute ?? 0), Seconds \(diffDateComponents.second ?? 0)"

    print(countdown)
}
like image 133
Marcelo dos Santos Avatar answered Nov 09 '22 22:11

Marcelo dos Santos


This works like a charm:

var releaseDate: NSDate?

override func viewDidLoad() {
    super.viewDidLoad()

    let releaseDateString = "2016-03-02 22:00:00"
    let releaseDateFormatter = NSDateFormatter()
    releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    releaseDate = releaseDateFormatter.dateFromString(releaseDateString)!
    
    NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "countDownDate", userInfo: nil, repeats: true)
}

func countDownDate() {
    let currentDate = NSDate()

    let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: currentDate, toDate: releaseDate!, options: .MatchFirst)
    
    let countdown = "Months: \(diffDateComponents.month), Days: \(diffDateComponents.day), Hours: \(diffDateComponents.hour), Minutes: \(diffDateComponents.minute), Seconds: \(diffDateComponents.second)"
    print(countdown)
}
like image 21
Dejan Skledar Avatar answered Nov 09 '22 22:11

Dejan Skledar


Swift 3

var releaseDate: Date? 

override func viewDidLoad() {
  super.viewDidLoad()     

  let releaseDateFormatter = DateFormatter()
  releaseDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  releaseDate = releaseDateFormatter.date(from:releaseDateString!)!

  Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.countDownDate), userInfo: nil, repeats: true)
}

func countDownDate() {
    let date = Date()
    let calendar = Calendar.current

    let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: date, to: releaseDate!)
    let countdown = "Days \(diffDateComponents.day),  Hours: \(diffDateComponents.hour), Minutes: \(diffDateComponents.minute), Seconds: \(diffDateComponents.second)"
    print(countdown)
}
like image 21
webjunkie Avatar answered Nov 10 '22 00:11

webjunkie