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

You can get the date for the first day of the current century in TypeScript/JavaScript using the Date object and some simple arithmetic. Here's an example function to achieve this:

index.ts
function getFirstDayOfCentury(): Date {
  const year = new Date().getFullYear(); // Get current year
  const centuryStartYear = Math.floor(year / 100) * 100; // Calculate current century start year
  return new Date(`${centuryStartYear}-01-01T00:00:00`);
}
257 chars
6 lines

The getFirstDayOfCentury function first gets the current year using the getFullYear method of the Date object. Then, it calculates the start year of the current century using some simple arithmetic.

Finally, it returns a Date object for the first day of the current century by creating a new Date object for January 1st of the century start year.

gistlibby LogSnag