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

137
Views
Get first day and last day in Month, Week and work week

I have a problem to get first day and last day in Month, Week and work week for JavaScript code.

For example selected date is 04/02/2022 (mm/dd/yyyy). The actual result will come out like below:

  1. Month (First day is 04/01/2022, last day is 04/30/2022)
  2. Week (First day is Sunday 03/27/2022 , last day is Saturday 04/02/2022)
  3. Work Week (First day is Monday 03/28/2022 , last day is Friday 04/01/2022)

This is my sample code:

//Get Month first day and last day. For example if date is 04/02/2022 (mm/dd/yyyy), the last day will show 04/01/2022 and the last day will show 04/30/2022

let selected_date = "Sat Apr 02 2022 00:00:00 GMT+0800 (Malaysia Time)";
let date = selected_date();
let firstDay = selected_date(date.getFullYear(), date.getMonth(), 1);
let lastDay = selected_date(date.getFullYear(), date.getMonth() + 1, 0); 
let selectedFirstDateCheck = moment(firstDay).format('MM/DD/YYYY');
let selectedLastDateCheck = moment(lastDay).format('MM/DD/YYYY');        
console.log ("First date:" + selectedFirstDateCheck + " Last date:" + selectedLastDateCheck);

//Get Week first day and last day. For example if date is Saturday 04/02/2022 (mm/dd/yyyy), the last day will show Sunday 03/27/2022 and the last day will show Saturday 04/02/2022
let curr = = "Sat Apr 02 2022 00:00:00 GMT+0800 (Malaysia Time)";
let  first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
let last = first + 6; // last day is the first day + 6

let firstday = new Date(curr.setDate(first)).toUTCString();
let lastday = new Date(curr.setDate(last)).toUTCString();

console.log ("First date:" + firstday + " Last date:" + lastday);


//Get Work Week first day and last day. For example if date is Saturday 04/02/2022 (mm/dd/yyyy), the last day will show Monday 03/28/2022 and the last day will show Friday 04/01/2022

let selected_date = "Sat Apr 02 2022 00:00:00 GMT+0800 (Malaysia Time)";
let firstday = new Date(selected_date.setDate(selected_date.getDate() - selected_date.getDay())).toUTCString();
let lastday = new Date(selected_date.setDate(selected_date.getDate() - selected_date.getDay() + 7)).toUTCString();
console.log ("First date:" + firstday + " Last date:" + lastday);

My codes result doesn't work to get the actual answer, anyone can guide me on how to solve this problem? Thanks.

about 4 years ago · Juan Pablo Isaza
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!