简体   繁体   English

从星期日开始工作日 - javascript

[英]start week day from sunday - javascript

I want to retrieve a date by providing day number of a specific week我想通过提供特定周的天数来检索日期

Eg例如

When I say当我说

day: 1

It should provide me:它应该为我提供:

2023-01-15

What I have tried so far is:到目前为止我尝试过的是:

 function calculatedDate (day){ let date = new Date(); let dayAtDate = date.getDay(); let dayDiff = day - dayAtDate; if(dayDiff < 0){ dayDiff = 7 + dayDiff; } let desiredDate = date.setDate(date.getDate() + dayDiff); return new Date(desiredDate); } console.log(calculatedDate(1));

Now the problem with above code is that it considers day: 1 as monday, but I want day: 1 to be sunday here.现在上面代码的问题是它认为day: 1是星期一,但我希望day: 1在这里是星期日。

Can anyone help me with the best possible way here?任何人都可以在这里以最好的方式帮助我吗?

Sunday is 0, Monday is 1 here:周日为 0,周一为 1:

 function calculatedDate (day) { let date = new Date(); let dayAtDate = date.getDay(); let dayDiff = day - (dayAtDate + 1); // change here if (dayDiff < 0) { dayDiff = 7 + dayDiff; } let desiredDate = date.setDate(date.getDate() + dayDiff); return new Date(desiredDate); } console.log(calculatedDate(1));

How about just -1 the provided day?在提供的那一天只 -1 怎么样? that should solve it:那应该解决它:

 function calculatedDate (day){ let date = new Date(); let dayAtDate = date.getDay(); day = day - 1; let dayDiff = day - dayAtDate; if(dayDiff < 0){ dayDiff = 7 + dayDiff; } let desiredDate = date.setDate(date.getDate() + dayDiff); return new Date(desiredDate); } console.log(calculatedDate(1));

You'd probably want to add some validation on the input parameter day , make sure it's a number and 1 or greater.您可能希望在输入参数day上添加一些验证,确保它是一个数字且大于或等于 1。

The current day will be 0, account for this by making the following change.当前日期将为 0,通过进行以下更改来说明这一点。

if(dayDiff < 0){
    dayDiff = 6 + dayDiff;
}

暂无
暂无

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

相关问题 在 Javascript 中获取周一开始和周日结束的一周开始和结束 - Get start and end of week with the start on Monday and end on Sunday in Javascript 一周的第一天和最后一天不工作(从星期日开始) - First and last day of the week not working (starting from sunday) JavaScript幻灯片显示(从其他幻灯片开始,具体取决于星期几) - JavaScript slideshow (start from other slide, depending on day of week) 请问如何在javascript中让工作日从星期一而不是星期日开始? - How to make the week days start at monday instead of sunday in javascript please? 从星期一而不是星期日开始 antd RangePicker (datepicker) 周 - start antd RangePicker (datepicker) week from monday instead of sunday 我应该如何从一周中“提取星期日”,然后将其作为一周的最后一天输入控制台 - how should I `extract Sunday` from the week and then enter it in console as the last day of the week 获取本周的开始日期和结束日期(每周从星期一开始,以星期日结束) - Get start date and end date of current week (week start from monday and end with sunday ) 检查nodejs中的星期几是否在星期一和星期日之间 - Check if day of week is between Monday and sunday in nodejs 在 Javascript 中获取一周中的天数,一周从星期日开始 - Get the days in a Week in Javascript, Week starts on Sunday 如何找到 9 周前的星期日,而不管今天是星期几使用 JavaScript - How to find sunday 9 weeks ago irrespective of what day of the week today is using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM