0

so i have timestamp for the begining of the year (Friday, 1 January 2021 00:00:00) and end of the year( Friday, 31 December 2021 23:59:59 )

{ yearStart: 1609455600000, yearEnd: 1640991599000 }

because i need to get data for every month in this year i need start and end timestamps of a month. is there any method for this? ty in advance

2 Answers2

1

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

Silvan Bregy
  • 2,544
  • 1
  • 8
  • 21
0

Use the following code to create a stored procedure or table-valued function. Format the output as desired.

declare @year varchar(4)
set @year = '2022'

; with cte as
 (select 0 'n' union select 1 union select 2 union select 3 union select 4 union select 5 union select 6
  union select 7 union select 8 union select 9 union select 10 union select 11)

select   dateadd(month, n, '1/1/'+@Year) 'yearStart'
, dateadd(second, -1, dateadd(month, n +1, '1/1/'+@Year)) 'yearEnd'
 from cte

enter image description here

tkeen
  • 362
  • 1
  • 8