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

To get the date for the first day of the current year in JavaScript, you can use the following code:

index.tsx
const now = new Date(); // create a new Date instance
const firstDayOfYear = new Date(now.getFullYear(), 0, 1); // set the month and day to 0 and 1 respectively
console.log(firstDayOfYear) // Output: Wed Jan 01 2020 00:00:00 GMT+0100 (Central European Standard Time)
267 chars
4 lines

Here, now.getFullYear() will give you the current year, and new Date(year, monthIndex, day) will create a new Date object with the specified year, month, and day. Setting the month index to 0 and the day to 1 will give you the first day of the current year.

gistlibby LogSnag