How to get current time with jQuery

Viewed 612188

The following returns time in microseconds, for example 4565212462.

alert( $.now() );

How do I convert it to a human readable time format, such as (hours:minutes:seconds)?

16 Answers

<p id="date"></p>

<script>
var d = new Date();
document.getElementById("date").innerHTML = d.toTimeString();
</script>

You can use Date() in JS.

The following

function gettzdate(){
    var fd = moment().format('YYYY-MM-DDTHH:MM:ss');
    return fd ; 
}

works for forcing the current date onto a <input type="datetime-local">

For local time in ISO8601 for SQL TIMESTAMP you could try:

var tzoffset = (new Date()).getTimezoneOffset() * 60000;
var localISOTime = (new Date(Date.now() - tzoffset))
  .toISOString()
  .slice(0, 19)
  .replace('T', ' ');
$('#mydatediv').val(localISOTime);

console.log(
  new Date().toLocaleString().slice(9, -3)
  , new Date().toString().slice(16, -15)
);

var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
document.write(time);

Related