I suggest to use a date and time module. For example dayjs
There are helpers like startOf('month')
or add()
.
Here is some pseudo code of the module regarding your problem. I suggest you to learn it as date and time is handy to now in lots of projects.
const dayjs = require('dayjs')
// Initialise dayjs object with your provided start of year timestamp.
const startOfYear = dayjs(1609455600000)
// set to start end endof month
const january_start = startOfYear.startOf('month')
const january_end = startOfYear.endOf('month')
// add one month and set to start resp. end of
const february_start = startOfYear.add(1, 'month').startOf('month')
const february_end = startOfYear.add(1, 'month').endOf('month')
// ... loop and go further..
// Helper function for getting back javascript dates.
const numeric_date = startOfYear.toDate().getTime()
// there is also a format() function which allows printing
// nice looking dates.
startOfYear.format('MMM DD YYYY') // => prints 01 Jan 2021
For more info read the docs