How do I get the current date in JavaScript?

Total
0
Shares

How do I get the current date in JavaScript?


Solution

Use new Date() to generate a new Date object containing the current date and time.

_x000D_

_x000D_

var today = new Date();_x000D_
var dd = String(today.getDate()).padStart(2, '0');_x000D_
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!_x000D_
var yyyy = today.getFullYear();_x000D_
_x000D_
today = mm + '/' + dd + '/' + yyyy;_x000D_
document.write(today);

_x000D_

_x000D_

_x000D_

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.

Source: StackOverflow.com

Leave a Reply

Your email address will not be published. Required fields are marked *