Show current date and time (real-time clock)

Asked

Viewed 1,672 times

1

I want to display on the system screen the current/current date and time, which shows the clock incrementing seconds in real time.

I’ve tried using the datetime.now (shows the date and time, however static, being the time when I loaded the page). DateTime.today (only shows the date). I used DateTime.utcNow also, but equals the first case.

controller:

public IActionResult RegistrarPonto()
        {
            var aux = new RegistroPontoViewModel { DateTime = DateTime.Now };
            return View(aux);
        }

view

    <div>
       <input asp-for="DateTime" type="datetime" class="form-control form-control-lg" readonly />
    </div>
  • https://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript

1 answer

2


What you want to do can’t be done with the .net. You want a real-time clock so you have to run on client for the browser and not on servidor. You can even recover the current date from your server if needed, but to keep the clock real time you will need to use javascript.

Take an example:

var timeDisplay = document.getElementById("time");


function refreshTime() {
  var dateString = new Date().toLocaleString("en-US", {timeZone: "America/Sao_Paulo"});
  var formattedString = dateString.replace(", ", " - ");
  timeDisplay.innerHTML = formattedString;
}

setInterval(refreshTime, 1000);
<p id="time"></p>

  • Thanks George, it worked right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.