get the current time in ddmmyy in javascript

You can use the Date() constructor to get the current date and time in javascript. Then you can use the getFullYear(), getMonth(), and getDate() methods to extract the year, month, and date respectively. Here's how you can get the current time in ddmmyy format:

const date = new Date();
const year = date.getFullYear().toString().slice(-2); // get the last two digits of the year
const month = (date.getMonth() + 1).toString().padStart(2, '0'); // add leading zero if month < 10
const day = date.getDate().toString().padStart(2, '0'); // add leading zero if day < 10

const ddmmyy = `${day}${month}${year}`;
console.log(ddmmyy); // e.g. "030921"
384 chars
8 lines

Note that getMonth() returns a zero-based index, hence the need to add 1 to the month and the use of padStart() to add a leading zero.

gistlibby LogSnag