Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 1.02 KB

README.md

File metadata and controls

37 lines (27 loc) · 1.02 KB

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