You can use the Date constructor which takes in a number of milliseconds and converts it to a JavaScript date:
var d = new Date(Date.now());
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
In reality, however, doing Date(Date.now()) does the same thing as Date(), so you really only have to do this:
var d = new Date();
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
Answer from Sumner Evans on Stack OverflowVideos
You can use the Date constructor which takes in a number of milliseconds and converts it to a JavaScript date:
var d = new Date(Date.now());
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
In reality, however, doing Date(Date.now()) does the same thing as Date(), so you really only have to do this:
var d = new Date();
d.toString() // returns "Sun May 10 2015 19:50:08 GMT-0600 (MDT)"
You can use Date().toISOString(), i.e.:
let d = new Date().toISOString();
document.write(d);
Output:
2024-05-22T12:19:33.038Z
Demo:
let d = new Date().toISOString();
document.write(d);
Use new Date() to generate a new Date object containing the current date and time.
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
document.write(today);
This will give you today's date in the format of mm/dd/yyyy.
Simply change today = mm +'/'+ dd +'/'+ yyyy; to whatever format you wish.
var utc = new Date().toJSON().slice(0,10).replace(/-/g,'/');
document.write(utc);
Use the replace option if you're going to reuse the utc variable, such as new Date(utc), as Firefox and Safari don't recognize a date with dashes.