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

date, javascript

Solution

const dateObj = new Date();
const month   = dateObj.getUTCMonth() + 1; // months from 1-12
const day     = dateObj.getUTCDate();
const year    = dateObj.getUTCFullYear();

const newDate = year + "/" + month + "/" + day;

// Using template literals:
const newDate = `${year}/${month}/${day}`;

// Using padded values, so that 2023/1/7 becomes 2023/01/07
const pMonth        = month.toString().padStart(2,"0");
const pDay          = day.toString().padStart(2,"0");
const newPaddedDate = `${year}/${pMonth}/${pDay}`;

or you can set new date and give the above values

Problem

`alert(dateObj)` gives `Wed Dec 30 2009 00:00:00 GMT+0800` How to get date in format `2009/12/30`?

Original source

Related problems