How to create a timer (chrono) with NSDateFormatter? - ios

This is a regular timer (chrono) with minutes and seconds.
It works fine but is there a way to have the same result with an NSDateFormatter.
var min = 0
var sec = 0
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60)
return String(format: "%2d:%02d",minutes, seconds)
}
func updateTimer(){
sec++
self.timeElapsed.text = self.stringFromTimeInterval(Double(sec))
}
Here is my attempt :
var sec = 0
func theTime(interval:NSTimeInterval) -> String {
var dateFormatter = NSDateFormatter()
var date = NSDate(timeIntervalSince1970: NSTimeInterval())
dateFormatter.dateFormat = "mm:ss"
return dateFormatter.stringFromDate(date)
}
func updateTheTime(){
sec++
self.newTimer.text = self.theTime(Double(sec))
}
I'm doing something wrong because it is always 00:00.
How can I make it work?

You can use NSDate and NSDateFormatter to get time string.
create an object of NSDateFormatter and NSDate, then set the style of your time to NSDateFormmatter, then call stringFromDate to get time string.
var dateFormatter = NSDateFormatter()
var date = NSDate(timeIntervalSince1970: interval)
dateFormatter.dateFormat = "mm-ss"
dateFormatter.stringFromDate(date)

You can use NSDateComponentsFormatter.
class ViewController: UIViewController {
// 1
var startDate: NSDate!
// 2
#IBOutlet weak var timerLabel: UILabel!
// 3
func tick() {
// 4
let elapsed = NSDate().timeIntervalSinceDate(startDate)
// 5
let formattedTime = NSDateComponentsFormatter().stringFromTimeInterval(elapsed)
// 6
timerLabel.text = formattedTime
}
// 7
#IBAction func startButtonPressed() {
// 8
startDate = NSDate()
// 9
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "tick", userInfo: nil, repeats: true)
}
}
Here's what's happening in that Swift code:
The startDate property will be initialized in startButtonPressed()
The timerLabel will show the output. It is a standard UILabel.
tick() will be called from an NSTimer
elapsed calculates how much time has transpired since the startDate. NSDate() is a handy way to get the current date.
This is where the magic happens. The NSDateComponentsFormatter will output a correctly trimmed String for the elapsed time.
The formattedTime is displayed on the screen
startButtonPressed() is called when the user taps the Start button.
The startDate property is set to the current date.
An NSTimer will call tick() every 1 second.
Here's a screenshot of what it looks like in practice with:
four seconds elapsed
one minute and four seconds elapsed

Related

Timer decrement time changing if device date changes in Swift

I am getting two dates from server. Current time and Unlock time.
Unlock date: 2021-07-23 05:55:44 +0000
Current date: 2021-07-23 05:54:44 +0000
So, I have to subtract from unlock date to current date and Remainder time, I have to run timer to unlock.
let client = TrueTimeClient.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
let when = DispatchTime.now() + 0.1
DispatchQueue.main.asyncAfter(deadline: when) {
self.countDownTimer = .scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
self?.countDownTime()
}
}
}
#objc func countDownTime() {
let ntpTime = client.referenceTime?.now()
let unlockDuration = self.getUnlockCountDownTime(currentTime: unlocksTime ?? "" , unlockTime: unlocksTime ?? "", ntpDate: ntpTime ?? Date())
unlockHrsLabel.text = "\(unlockDuration)"
if unlockDuration == "0d :0h : 0: 0" {
self.stopTimer()
//call some api
}
}
func getUnlockCountDownTime(currentTime: String, unlockTime: String, ntpDate: Date) -> String {
let dateFormatter = DateFormatter()
let loc = Locale(identifier: "en_US_POSIX")
dateFormatter.locale = loc
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
// dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let unlockDate = dateFormatter.date(from: "\(unlockTime)") ?? Date()
print("unlockDate \(unlockDate)")
print("ntpDate \(ntpDate)")
let currentDate = dateFormatter.date(from: "\(currentTime)") ?? Date()
print("currentDate \(currentDate)")
let calendar = Calendar.current
let diffDateComponents = calendar.dateComponents([.day, .hour, .minute, .second], from: unlockDate, to: ntpDate)
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 stopTimer(){
guard self.countDownTimer != nil else {
fatalError("No timer active, start the timer before you stop it.")
}
self.countDownTimer?.invalidate()
}
Here, I have used pod 'TrueTime' to fetch ntp time, but if we change device time, the timer duration increasing automatically.
Suppose, i am getting remainder time 1:50 seconds, If I change date to june 20, 2021, Its showing more days and hours to unlock.
I have to show always unlock timer duration same irrespective time changes and time zones.
It should come as above screenshot. But, if I change date, it is coming as below screen which is wrong
How to fix this? Any suggestions?
I have found solution.
I just uninstalled pod 'TrueTime' and subtract current time to unlock time using timeinterval Then I have run timer with remainder seconds.
Then I run timer with decrement of remainder.
#objc func countDownTime() {
self.remainingTime -= 1
seunlockHrsLabel.text = "\(self.convertIntegerToFormat(remainingTime: self.remainingTime))"
if remainingTime == 0 {
self.stopTimer()
}
}
func convertIntegerToFormat(remainingTime: Int) -> String {
let days = remainingTime / (24 * 3600)
let hours = remainingTime / 3600
let seconds = remainingTime % 60
let minutes = (remainingTime / 60) % 60
return "\(days): \(hours): \(minutes): \(seconds)"
}
Now, It is working irrespective of device time and time zones.

Sync multiple time labels with host time

What would be a possible way to update labels that displays static time received once from API.I have a tableView where each cell displays city name current temperature and time just like the native iPhone WeatherApp.Is there a way to observe the device clock minutes so I can trigger some code to update the times when a minute goes by?
You can subclass a UILabel and add a timer to it so that it autoupdates itself:
Considering your last question where you get the timeZone offset from GMT from your API, you can subclass a UILabel, add a timeZone property to it and a didSet closure to setup a timer to fire at the next even minute with a 60 seconds interval and set it to repeat. Add a selector to update its label every time this method is called:
class Clock: UILabel {
let dateFormatter = DateFormatter()
var timer = Timer()
var timeZone: Int = 0 {
didSet {
dateFormatter.timeStyle = .short
dateFormatter.timeZone = TimeZone(secondsFromGMT: timeZone)
var components = Date().components
components.minute! += 1
components.second = 0
components.nanosecond = 0
timer = .init(fireAt: components.date!, interval: 60, target: self, selector: #selector(update), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: .common)
update()
}
}
#objc func update(_ timer: Timer? = nil) {
text = dateFormatter.string(from: Date())
}
}
extension Date {
var components: DateComponents {
Calendar.current.dateComponents(in: .current, from: self)
}
}
Now you can just create your clock label and when you setup its timeZone property it will start running automatically:
let clock: Clock = .init(frame: .init(origin: .zero,
size: .init(width: 200, height: 30)))
clock.timeZone = -14400
clock.text // "11:01 PM"

Find difference in two dates in iOS

I am not sure what I am doing wrong, I need to find difference between two dates and extract seconds from it, below is my code. I am not getting correct seconds. There is difference of seconds.
public func captureStartTime() {
captureStartDateTime = Date()
}
public func captureEndTime(eventType: String, eventElement: String) {
let difference = Date().timeIntervalSince(captureStartDateTime)
let interval = Int(difference)
let seconds = interval % 60
let secondsDescrp = String(format: "%02d", seconds)
}
interval is the answer you want. That is the total number of seconds between the two dates.
Your seconds value would only be useful if you wanted to calculate the number of hours, minutes, and seconds or the number of minutes and seconds from the total number of seconds.
Use the following code to get the difference between two dates, Store current time in startTime when pressed button 1 and store current date time in endTime when pressed button 2, See this code, I hope this helps you.
var startTime:Date!
var endTime:Date!
#IBAction func buttonStartTime(_ sender: UIButton) {
startTime = Date()
}
#IBAction func buttonEndTime(_ sender: UIButton) {
endTime = Date()
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.second]
formatter.unitsStyle = .full
let difference = formatter.string(from: startTime, to: endTime)!
print(difference)//output "8 seconds"
}
Output
8 seconds
you can also use default date components and according to that compare your dates and you can get the difference in year, month, day etc
let dateString1 = "2019-03-07T14:20:20.000Z"
let dateString2 = "2019-03-07T14:20:40.000Z"
//set date formate
let Dateformatter = DateFormatter()
Dateformatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//convert string to date
let dateold = Dateformatter.date(from: dateString1)!
let datenew = Dateformatter.date(from: dateString2)!
//use default datecomponents with two dates
let calendar1 = Calendar.current
let components = calendar1.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateold, to: datenew)
let seconds = components.second
print("Seconds: \(seconds)")

Displaying Live Time Label in Swift 4?

My time label is displaying the time, when I open my app but it won't update it live. I had a look at other answers but they didn't make sense.
// CURRENT TIME
#IBOutlet weak var currentTimeLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
getCurrentTime()
}
// FORMAT TIME
func getCurrentTime(){
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm"
let str = formatter.string(from: Date())
currentTimeLabel.text = str
}
I want my app to update the time label live. Thanks in advance. It's probably a really simple fix.
Use Timer for your requirement,
class ViewController: UIViewController {
#IBOutlet weak var currentTimeLabel: UILabel!
var timer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
getCurrentTime()
}
private func getCurrentTime() {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(self.currentTime) , userInfo: nil, repeats: true)
}
#objc func currentTime() {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm"
currentTimeLabel.text = formatter.string(from: Date())
}
}
You can create Timer with repeating every minute (because you don't need seconds for anything) starting in the next minute (so call getCurrentTime() once before you start Timer).
Every minute code inside timer's closure gets executed so you can say that you want to call getCurrentTime(). Now your currentTimeLabel will be updated every minute
let now = Date()
let date = Calendar.current.date(bySettingHour: Calendar.current.component(.hour, from: now), minute: Calendar.current.component(.minute, from: now) + 1, second: 0, of: now)!
let timer = Timer(fire: date, timeInterval: 60, repeats: true) { _ in
self.getCurrentTime()
}
Also I would recommend you to have formatter variable outside of the method (in global scope)
lazy var formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm" // or "hh:mm a" if you need to have am or pm symbols
return formatter
}()
and then in getCurrentTime() just get String and change text of currentTimeLabel
func getCurrentTime() {
currentTimeLabel.text = formatter.string(from: Date())
}

Swift CountDown Days Until from Date Picker

I have been struggling making a countdown in Swift where it shows only the days left until some date where the input is the DatePicker... I have creo experience with Swift so, I have been struggling for a while. I tried some similar answers here but didn't work, I watched a tutorial but is a normal countdown with months, days, minutes and seconds, this is the code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var timeLabel: UILabel!
let formatter = DateFormatter()
let userCleander = Calendar.current;
let requestedComponent : Set<Calendar.Component> = [
Calendar.Component.month,
Calendar.Component.day
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timePrinter), userInfo: nil, repeats: true)
timer.fire()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func timeCalculator(dateFormat: String, endTime: String, startTime: Date = Date()) -> DateComponents {
formatter.dateFormat = dateFormat
let _startTime = startTime
let _endTime = formatter.date(from: endTime)
let timeDifference = userCleander.dateComponents(requestedComponent, from: _startTime, to: _endTime!)
return timeDifference
}
func timePrinter() -> Void {
let time = timeCalculator(dateFormat: "MM/dd/yyyy a", endTime: "12/25/2018 a")
timeLabel.text = "\(time.month!) Months \(time.day!) Days"
}
}
Several things: Don't use strings to compare dates. Use Date objects and Calendar operations. (More on that in a second.)
Don't run a timer once a second. Save the current date to user defaults. When your app is launched, compare the saved date to the current date and see if the day has changed.
When running, listen for UIApplicationSignificantTimeChange notifications, and when you get one, check to see if the date has changed.
As for comparing the current date to the user-selected date, you've got the right idea using dateComponents(_:from:to:), but you should pass in components of just [.day].
EDIT:
Code like this would do the trick:
override func viewDidLoad() {
super.viewDidLoad()
//Set up the date picker to pick dates, not dates & times
datePicker.datePickerMode = .date
//Force the date picker to use midnight today as it's base date and
//to pick a date at least 1 day in the future
guard let today = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date()),
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today)
else {
return
}
datePicker.minimumDate = tomorrow
datePicker.date = tomorrow
}
#IBAction func datePickerChanged(_ sender: UIDatePicker) {
let future = datePicker.date
//Use midnight today as the starting date
guard let today = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date()) else { return }
//Calculate the number of days between today and the user's chosen day.
let difference = Calendar.current.dateComponents([.day], from: today, to: future)
guard let days = difference.day else { return }
let ess = days > 1 ? "s" : ""
infoLabel.text = "That date is \(days) day\(ess) away."
}

Resources