Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
How to get start and end timestamps of every month in selected year

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!