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

149
Views
Obtenga el primer día y el último día en el mes, la semana y la semana laboral

Tengo un problema para obtener el primer día y el último día en Mes, Semana y semana laboral para el código JavaScript.

Por ejemplo, la fecha seleccionada es 02/04/2022 (mm/dd/aaaa). El resultado real saldrá como a continuación:

  1. Mes (el primer día es el 01/04/2022, el último día es el 30/04/2022)
  2. Semana (El primer día es el domingo 27/03/2022, el último día es el sábado 02/04/2022)
  3. Semana laboral (el primer día es el lunes 28/03/2022, el último día es el viernes 01/04/2022)

Este es mi código de muestra:

 //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);

El resultado de mis códigos no funciona para obtener la respuesta real, ¿alguien puede guiarme sobre cómo resolver este problema? Gracias.

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!