How to get day/month/year from a date object in JavaScript?

How to format date object in JavaScript
Photo Credit: unsplash/steve_j

How to format a date in JavaScript or How to get date in format 25/05/2022 in JavaScript? These are typical questions that we may need to answer when manipulating date objects using JavaScript. There is no direct function to format a date, here is what we can do to format a date object into "dd/MM/yyyy":

var currentDate = new Date();
var month = currentDate.getUTCMonth() + 1; //getUTCMonth() returns 0-11
var day = currentDate.getUTCDate();
var year = currentDate.getUTCFullYear();
var dateFormatted = day + "/" + month + "/" + year;

Here is a list of the methods that can be used for getting information from a date object:

Method Description
getFullYear() Get the year as a four digit number (yyyy)
getMonth() Get the month as a number (0-11)
getDate() Get the day as a number (1-31)
getHours() Get the hour (0-23)
getMinutes() Get the minute (0-59)
getSeconds() Get the second (0-59)
getMilliseconds() Get the millisecond (0-999)
getTime() Get the time (milliseconds since January 1, 1970)
getDay() Get the weekday as a number (0-6)
Date.now() Get the time.

Post a Comment

Previous Post Next Post