Get Current Date in Different Date Formats in JavaScript

In this article we will see how to get current date in JavaScript and in different formats like mm-dd-yyyy or dd-mm-yyyy.

In JavaScript there is not an in-built methods or an easy way to get current date in required date formats. To get the date, moth and year we have to use getDate(), getMonth() and getFullYear() methods which will extract a specific date, month and year from the new Date() value.

Using this value we can arrange the results in a particular date format as we require.

Using Vanilla JavaScript

In this example we will use vanilla JavaScript methods to get a required date format.

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;  // index starts with 0 so added 1 to it
var yyyy = today.getFullYear();
    dd='0'+dd; // add a 0 if day a single digit

    mm='0'+mm; // add a 0 if month a single digit
today = mm+'-'+dd+'-'+yyyy;
console.log(today); // 11-22-2021
today = dd+'-'+mm+'-'+yyyy;
console.log(today); // 22-11-2021

new Date() method give us date with time in milliseconds of the current day.

getDate() : This method extracts the date of the month of a current date according to its locale time. The return value is an integer.

getMonth() : This method in JavaScript return us the month of a specific date according to its Locale time. The return value is an integer which start from 0 and end at 11 i.e January will be 0 and Feb will be 1.

getFullYear() method return the year of a specific date according to Locale time. The return value is a four digit number starting from 1000 and 9999.

Using momentjs

momentjs is a JavaScript library which help us to display date and time very easily. To get the mm-dd-yyyy date format we write the following code:


Related Topics:

Code On How To Get Yesterday Date In JavaScript

Add Days To A Date In JavaScript With Example

How to Get Tomorrow's Date using JavaScript - Quick Way

Convert a Unix Timestamp To Time In JavaScript

💾 Recommended Articles