获取从日期算起的月份的周号(从星期一开始的周) [英] Get week number of the month from date (weeks starting on Mondays)

查看:109
本文介绍了获取从日期算起的月份的周号(从星期一开始的周)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用JavaScript查找从给定日期开始的月份的周号。每周的开始时间是星期一。



我尝试了下面的代码,但未获得准确的结果。



  function getWeekNumber(date){var monthStartDate = new Date(new Date()。getFullYear(),new Date()。getMonth(),1); monthStartDate =新日期(monthStartDate); var day = monthStartDate.getDay();日期=新日期(日期); var date = date.getDate();让weekNumber = Math.ceil((date +(day))/ 7);返回(weekNumber == 0)? 1:weekNumber;} var week = getWeekNumber('2020-04-04'); console.log(week);  

p>

解决方案

您可以找到从星期一开始的几周的月份的周数(与 ISO周日期系统),方法是将输入日期回滚到上一个星期一,然后将星期一日期除以7,然后四舍五入以确定该日期属于月份的哪个星期。



此方法将正确处理一个月初的日期,而实际上是在上个月的最后一周。例如,2020-04-04是从2020-03-30(星期一)开始的一周中的星期六,因此它应该返回第5周,因为它是3月5日的一部分(而不是3月的第1周的一部分) 4月始于2020-04-06,即4月的第一个星期一)。



例如(分割一开始的位只是解析日期字符串,而不是依靠 new Date()来解析字符串,因为由于浏览器不一致而不建议这样做):



  const monthWeek =(s)=> {让[y,m,d] = s.split(’-’); //解析日期字符串let date = new Date(y,m-1,d); //创建日期对象date.setDate(d-((date.getDay()+ 6)%7)); //将日期调整为上一个星期一返回Math.ceil(date.getDate()/ 7); //返回月份的星期数} console.log(monthWeek('2020-04-04')); // 5console.log(monthWeek('2020-04-07')); // 1  


I have to find week number of the month from given date using JavaScript. Week start is Monday.

I have tried the code below but not getting accurate result.

function getWeekNumber(date) {
  var monthStartDate = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
  monthStartDate = new Date(monthStartDate);
  var day = monthStartDate.getDay();
  date = new Date(date);
  var date = date.getDate();

  let weekNumber = Math.ceil((date + (day)) / 7);
  return (weekNumber == 0) ? 1 : weekNumber;
}

var week = getWeekNumber('2020-04-04');
console.log(week);

解决方案

You could find the week number of the month for weeks starting on Monday (in line with the ISO week date system) by rolling the input date back to the previous Monday and then dividing the Monday date by 7 and rounding up to determine which week of the month the date falls in.

This approach will properly handle dates at the beginning of a month which actually fall in the last week of the previous month. For instance, 2020-04-04 is a Saturday in the week starting on 2020-03-30 (Monday), so it should return week 5 since it is part of the 5th week of March (and not part of the 1st week of April which starts on 2020-04-06, the first Monday in April).

For example (the split bit at the beginning is just to parse the date string rather than relying on new Date() to parse the string since that is not recommended due to browser inconsistencies):

const monthWeek = (s) => {
  let [y, m, d] = s.split('-'); // parse date string
  let date = new Date(y, m - 1, d); // create date object
  date.setDate(d - ((date.getDay() + 6) % 7)); // adjust date to previous Monday
  return Math.ceil(date.getDate() / 7); // return week number of the month
}

console.log(monthWeek('2020-04-04'));
// 5
console.log(monthWeek('2020-04-07'));
// 1

这篇关于获取从日期算起的月份的周号(从星期一开始的周)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆