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.tsx344 chars15 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