-1

*I have two dates. Those, I have to do date format in UTC and after that have to do subtraction of two dates. I have to run timer for remaining time. And once its reaches 0 second, I have to stop timer and I have to call some api.

fromDate: 2021-07-21T09:04:38.306Z, toDate: 2021-07-21T09:06:08.000Z

 override func viewDidLoad() { super.viewDidLoad() let when = DispatchTime.now() + 0.1 // change 2 to desired number of seconds DispatchQueue.main.asyncAfter(deadline: when) { self.countDownTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(MyViewController.countDownTime), userInfo: nil, repeats: true) } } @objc func countDownTime() { let unlockDuration = self.getCountDownTime(currentTime: "2021-07-21T09:04:38.306Z" , unlockTime: "2021-07-21T09:06:08.000Z") if unlockDuration == "0d :0h : 0: 0" { self.stopTimer() } } func stopTimer(){ guard self.countDownTimer != nil else { fatalError("No timer active, start the timer before you stop it.") } self.countDownTimer?.invalidate() self.callAPI() } func getCountDownTime(currentTime: String, unLockTime: String) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ" dateFormatter.timeZone = TimeZone(abbreviation: "UTC") let unlockDate = dateFormatter.date(from: "\(unlockTime)") ?? Date() let currentDate = dateFormatter.date(from: "\(currentTime)") ?? Date() print(currentDate) print(unlockDate) let calendar = Calendar.current let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: unlockDate, to: currentDate) let countdown = "\(String(describing:diffDateComponents.day!))d :\(String(describing: diffDateComponents.hour!))h : \(String(describing: diffDateComponents.minute!)): \(String(describing: diffDateComponents.second!))" print(countdown) return countdown } func callAPI() { //Calling api here } 

But, Always its printing as 0d :0h : 0: 0 And its not showing seconds, minutes Its printing like below always

2021-07-21 11:14:23 +0000 2021-07-21 11:14:23 +0000 0d :0h : 0: 0 

Any Suggestions?*

9
  • Updated, pls check @CSmith Commented Jul 21, 2021 at 11:31
  • 1
    Clearly dateFormatter.date(from:) is returning nil. Try changing our dateFormatter.dateFormat to "yyyy-MM-dd'T'HH:mm:ss.SSSZ" Commented Jul 21, 2021 at 11:34
  • I tried with above suggested format by you @CSmith, BUT STILL SHOWING like 2021-07-21 11:36:47 +0000, 2021-07-21 11:36:47 +0000, 0d :0h : 0: 0 in console Commented Jul 21, 2021 at 11:37
  • I bet you'll find that dateFormatter.date(from: "\(unlockTime)") is returing nil. Commented Jul 21, 2021 at 11:40
  • 1
    The corrected format string suggested by @CSmith worked for me Commented Jul 21, 2021 at 11:47

1 Answer 1

1

To parse a date with this format:

2021-07-21T09:04:38.306Z

use

dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" 

Your are getting a difference of 0 because dateFormatter.date(from: "\(unlockTime)") is returning nil, so your subtracting Date() from Date() and getting a 0 difference.

Sign up to request clarification or add additional context in comments.

3 Comments

format is working fine now to me which was suggested by you, But, its not decrementing the time for each second. Always printing 0d :0h : -1: -29 till infinity
determining the time difference between two Date() is a widely answered problem (see stackoverflow.com/questions/4371757/…). Its not clear to me what you're actually trying to do but now that your dateFormat is fixed I'm sure you'll figure it out.
Also, don’t forget to set the locale for the date formatter to Locale(identifier: "en_US_POSIX")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.