Javascript ISO格式的每月周数(如果星期四交叉,则周计数)
根据ISO标准,我每个月需要一个周数 而且我使用的是momentJS,而不是Js的日期 根据ISO日期格式,如果星期四是该月的一周,则该周为该月的一周,否则为上一个月的一周 根据今年四舍五入周数为5的月数: 火星、五月、八月和十一月 其余的据说有4周 以下是我的代码灵感: 编辑如果我没有在评论中应用建议的修复,我将在2018年12月结束1周,2017年也一样Javascript ISO格式的每月周数(如果星期四交叉,则周计数),javascript,angular,date,momentjs,Javascript,Angular,Date,Momentjs,根据ISO标准,我每个月需要一个周数 而且我使用的是momentJS,而不是Js的日期 根据ISO日期格式,如果星期四是该月的一周,则该周为该月的一周,否则为上一个月的一周 根据今年四舍五入周数为5的月数: 火星、五月、八月和十一月 其余的据说有4周 以下是我的代码灵感: 编辑如果我没有在评论中应用建议的修复,我将在2018年12月结束1周,2017年也一样 ngOnInit() { this.generateTimelineForGivenYear('2018'); } generate
ngOnInit() {
this.generateTimelineForGivenYear('2018');
}
generateTimelineForGivenYear(year){
for (let i = 1; i < 13; i++) {
let month;
if (i < 10) month = '0'.concat(i.toString());
else month = i;
this.howManyWeeksForGivenMonth(moment(year + '-'+ month + '-05'));
}
}
howManyWeeksForGivenMonth(myDate){
console.log('The Month : ', myDate.month() + 1);
const start = myDate.clone().startOf('month').week();
let end = myDate.clone().endOf('month').week();
if( start > end ){ end = 52 + end }
const res = end - start;
console.log('The Number of Weeks: ', res);
console.log('----------------------------------------------------');
}
正如你所看到的,我得到的两个月是5周,而且也不是正确的两个月:
九月和十二月
我也试过这个:
howManyWeeksForGivenMonth(myDate){
console.log('The Month : ', myDate.month() + 1);
const first = myDate.day() == 0 ? 6 : myDate.day()-1;;
const day = 7-first;
const last = myDate.daysInMonth();
let res = Math.floor((last-day)/7);
if ((last-day) % 7 !== 0) res++;
console.log('The Number of Weeks: ', res);
console.log('----------------------------------------------------');
}
其中给出:
The Month : 1
The Number of Weeks: 4
----------------------------------------------------
The Month : 2
The Number of Weeks: 3
----------------------------------------------------
The Month : 3
The Number of Weeks: 4
----------------------------------------------------
The Month : 4
The Number of Weeks: 4
----------------------------------------------------
The Month : 5
The Number of Weeks: 5
----------------------------------------------------
The Month : 6
The Number of Weeks: 4
----------------------------------------------------
The Month : 7
The Number of Weeks: 4
----------------------------------------------------
The Month : 8
The Number of Weeks: 5
----------------------------------------------------
The Month : 9
The Number of Weeks: 4
----------------------------------------------------
The Month : 10
The Number of Weeks: 4
----------------------------------------------------
The Month : 11
The Number of Weeks: 4
----------------------------------------------------
The Month : 12
The Number of Weeks: 4
----------------------------------------------------
…不是更好
我做错了什么
更新:
一旦发现我没有在周四削减开支是个问题,下面是我的新尝试:
howManyWeeksForGivenMonth(myDate){
console.log('The Month ', myDate.month() + 1 );
let weeks = 4
if(myDate.clone().startOf('month').weekday() >= 5 ){
weeks ++;
console.log('first week extra');
}
if(myDate.clone().endOf('month').weekday() < 5 ) {
weeks ++;
console.log('last week extra');
}
return weeks;
}
这里面没什么可读的,简直是垃圾
很明显,我知道这两个“如果”在一个月内不应该同时触发,但我认为这根本不会发生
我想错了,但除此之外,它仍然不符合正确的月份。那么现在是什么呢 基于周的日历有一些属性。 第四天始终是基于周和基于月的日历(月份或年份)的一部分。时间段结束前的第四天相同
function getWeeksInMonth(year, month)
{
// 4th day of the month
const start_date = new Date(year, month, 4);
// 1st day of next month -4 day
const end_date = new Date(year, month + 1, -3);
// (0 || 7) => 7, it's a fix: ISO sundays have a number of 7, not 0
// how much days must be added at the beginning
const start_delta = (start_date.getDay() || 7) - 1;
// how much days must be added at the end
const end_delta = 7 - (end_date.getDay() || 7);
// how much days are between the two dates
// replace knowing if the month is 28/29/30/31 days long
const between_delta = Math.round((end_date - start_date) / 86400000);
// divided by 7 to get the number of weeks
return Math.floor((start_delta + between_delta + end_delta) / 7);
}
我不遵循你确定一个月的周数的方法。我的方法是计算第一个星期四之后剩下的天数:如果有28天或更多,则有5周。否则,有4个 /*@param{number}年-全年 **@param{number}month-日历月数Jan=1、Feb=2等。 **@returns每月返回{number}周 */ 功能周/月/月{ //创建给定年份和月份的日期 var d=新日期年,第1个月,第1个月; //获取该月的天数 var daysInMonth=新日期年,月,0.getDate; //获取第一个星期四的日期 var firstThuDate=11-d.getDay%7+1; //获取第一个星期四之后的月份天数 var daysLeft=daysInMonth-firstThuDate; //如果还剩28天或更多,则有5周 //否则,有4个 返回天数ft>27?5:4; } //2018年的几周和几个月
对于var i=0;我明白了:我用的是月初和月底,而不是从星期四开始和星期四之前计数。这就是造成差异的原因。现在我只需要弄清楚如何判断给定的一周是否跨越了周四。我发现:啊,MometJS有一个工作日函数!容易多了!对不起,不是这样的。我正在研究我自己的答案。重读我的评论。
The Month 1
last week extra
5
The Month 2
last week extra
5
The Month 3
4
The Month 4
last week extra
5
The Month 5
last week extra
5
The Month 6
first week extra
5
The Month 7
last week extra
5
The Month 8
4
The Month 9
first week extra
last week extra
6
The Month 10
last week extra
5
The Month 11
4
The Month 12
first week extra
last week extra
6
function getWeeksInMonth(year, month)
{
// 4th day of the month
const start_date = new Date(year, month, 4);
// 1st day of next month -4 day
const end_date = new Date(year, month + 1, -3);
// (0 || 7) => 7, it's a fix: ISO sundays have a number of 7, not 0
// how much days must be added at the beginning
const start_delta = (start_date.getDay() || 7) - 1;
// how much days must be added at the end
const end_delta = 7 - (end_date.getDay() || 7);
// how much days are between the two dates
// replace knowing if the month is 28/29/30/31 days long
const between_delta = Math.round((end_date - start_date) / 86400000);
// divided by 7 to get the number of weeks
return Math.floor((start_delta + between_delta + end_delta) / 7);
}