Created by Samuel MeddowsLinked to 78.8m issues across 68 teams
To get the current date in JavaScript, you can use the new Date()
method. This will generate a Date
object containing the current date and time. To format the date, you can use the getDate()
, getMonth()
, and getFullYear()
methods.
To get the date in the format of mm/dd/yyyy, you can use the following code:
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 the current date in the format of mm/dd/yyyy. If you wish to format the date differently, simply change the today = mm +'/'+ dd +'/'+ yyyy;
line to whatever format you desire.