get the date for the first day of the current century in javascript

To get the date for the first day of the current century in Javascript, you can use the Date constructor and a combination of math methods to calculate the year.

index.tsx
// create a new date object
let date = new Date();

// get the current year
let year = date.getFullYear();

// calculate the first year of the current century
let firstYear = Math.floor(year / 100) * 100;

// set the date to January 1st of the first year of the century
date.setFullYear(firstYear, 0, 1);

// output the date
console.log(date);
344 chars
15 lines

This code first creates a new Date object. It then gets the current year using the getFullYear() method. Using Math.floor() to round down to the nearest century, we calculate the year of the first year of the current century. We set the FullYear property of the Date object to the first year of the century, and then set the month and day to 0 and 1 respectively, which corresponds to January 1st. Finally, we output the date using console.log().

gistlibby LogSnag