Home > Back-end >  Displaying real time clock with date and time
Displaying real time clock with date and time

Time:07-24

I want to display real time update of clock with date and time in my app but if i go with the firebase real time update data just for clock it's too sloppy and wasted for my app.

Second option is to use Button to do the update but i can't just keep clicking this button to update the date. Can i have a solution where i can automate this button event or another different solution from this button to get my design done?

  @State var dateNow = Date()
    var body: some View {
    Button {
        dateNow = Date()} label: {
        Text("\(dateNow)")
    }

CodePudding user response:

Or you can just simply use TimelineView. This view automatically update the sub view for every specific time. In this case, it updates every 1 second.

TimelineView(.periodic(from: .now, by: 1)) { currentTime in
        Text("\(currentTime.date)")
}
  • Related