简体   繁体   English

如何在javascript中获得每个月第一个即将到来的星期四?

[英]How do I get the first upcoming thursday in everymonth in javascript?

What I want to do is get the date of today and then find the first Thursday of the month, if that day has already passed in this month then get the date of the first Thursday of the next month.我想要做的是获取今天的日期,然后找到该月的第一个星期四,如果该日期在本月已经过去,则获取下个月的第一个星期四的日期。

I'll provide an example due to me being a bit vague.由于我有点含糊,我将提供一个示例。 Let us say that the first Thursday of the month is on the 2nd of May and its the 1st of may right now.假设本月的第一个星期四是 5 月 2 日,现在是 5 月 1 日。 In that case, I would want to get the date of that Thursday due to it being the upcoming one.在这种情况下,我想知道那个星期四的日期,因为它是即将到来的日期。 But let's say it was the 13th of May and that date has already past then I would like to get the date of the next first Thursday of the coming month.但是假设是 5 月 13 日,那个日期已经过去了,那么我想知道下个月的第一个星期四的日期。

Try this尝试这个

 const firstThur = d => { const firstThur = new Date(d.getFullYear(),d.getMonth(),1,15,0,0,0); // first of the month for (let i=1;i<7;i++) { if (firstThur.getDay() === 4) break; firstThur.setDate(firstThur.getDate()+1) } return firstThur; }; const getThursday = () => { const now = new Date(); now.setHours(15,0,0,0); // normalise let ft = firstThur(now); return now.getTime() <= ft.getDate ? ft : firstThur(new Date(now.getFullYear(),now.getMonth()+1,1,15,0,0,0)) }; console.log(getThursday())

You can create a date for the first of the month, then move it to the first Thursday.您可以为每月的第一天创建一个日期,然后将其移至第一个星期四。 Test it against the passed in date and if it's earlier, get the first Thursday for the following month.根据传递的日期对其进行测试,如果它更早,则获取下个月的第一个星期四。

The following assumes that if the passed in date is the first Thursday, that Thursday should be returned.以下假设如果传入的日期是第一个星期四,则应返回该星期四。 It also assumes that the passed in date has its hours zeroed, otherwise if it's the first Thursday it will return the first in the following month.它还假设传入的日期将其小时数归零,否则如果是第一个星期四,它将在下个月返回第一个。

It also just sets the date to the first Thursday, so no looping to find the day.它还只是将日期设置为第一个星期四,因此无需循环查找日期。

 function getFirstThursday(date = new Date()) { // Create date for first of month let d = new Date(date.getFullYear(), date.getMonth()); // Set to first Thursday d.setDate((12 - d.getDay()) % 7); // If before date, get first Thursday of next month if (d < date) { d.setMonth(d.getMonth() + 1, 1); return getFirstThursday(d); } else { return d; } } // Some tests let f = d => d.toLocaleString('en-gb', { day:'2-digit',weekday:'short',month:'short' }); [new Date(2020,8, 1), // 1 Sep -> 3 Sep new Date(2020,8, 2), // 2 Sep -> 3 Sep new Date(2020,8, 3), // 3 Sep -> 3 Sep new Date(2020,8,24), // 24 Sep -> 1 Oct new Date(2020,9, 1), // 1 Oct -> 1 Oct new Date(2020,9, 2), // 2 Oct -> 5 Nov new Date(2020,9,26), // 26 Oct -> 5 Nov new Date(2020,9,27) // 27 Oct -> 5 Nov ].forEach( d => console.log( `${f(d)} => ${f(getFirstThursday(d))}` ));

The else block is redundant, the second return could just follow the if but it makes things clearer I think. else块是多余的,第二个返回可以跟在if 之后,但我认为它使事情变得更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM