I am trying to set the live DateTime in blazor webpages. I tried using Datetime.now . it's not updating the live time. I want to do it without using javascript.
Any help regarding this issue?
You need a Timer. Shortest sample I could think of:
@page "/"
@using System.Threading
@implements IDisposable
<p>@theTime</p>
@code{
string theTime;
Timer aTimer;
protected override void OnInitialized()
{
aTimer = new Timer(Tick, null, 0, 1000);
}
private void Tick(object _)
{
theTime = DateTime.Now.ToLongTimeString();
InvokeAsync(StateHasChanged);
}
public void Dispose()
{
aTimer?.Dispose();
}
}