How to format date value in JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Example

Following is the code to format date value in JavaScript −

function changeDateFormat(dateValues, addToFront = "") {
   return typeof dateValues == "object" ? addToFront + dateValues.toLocaleDateString() : "";
}
var currentDate = new Date();
console.log("The current date values=" + currentDate);
console.log("After formatting date values=" +
changeDateFormat(currentDate));

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo289.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo289.js
The current date values=Sat Aug 29 2020 12:51:06 GMT+0530 (India Standard Time)
After formatting date values=8/29/2020
raja
Published on 09-Nov-2020 13:12:36
Advertisements