Skip to content

This is a sample app which will show the current time using timer which will show UI updates for every second.

License

Notifications You must be signed in to change notification settings

sunil-targe/CurrentTimeSwiftUI

Repository files navigation

CurrentTimeSwiftUI

This is a sample app which will show the current time using timer which will show UI updates for every second.

alt text

struct ContentView: View {
    @State var currentTime = getCurrentTime()
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

    var body: some View {
        Text(currentTime)
        .font(.system(.largeTitle))
        .foregroundColor(.gray)
        .onReceive(timer) { _ in
            self.currentTime = getCurrentTime()
        }
    }
}

func getCurrentTime() -> String {
    let formatter = DateFormatter()
    formatter.dateFormat = "h:mm:ss a"
    let dateString = formatter.string(from: Date())
    return dateString
}

Author

Sunil Targe • Creator of ॐ • Om chanting with Ninja App

License

MIT license

About

This is a sample app which will show the current time using timer which will show UI updates for every second.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages