如何在给定时间内找到周一
How to find Monday with given time?
无论哪一天,我都在努力寻找周一。它有两个要求。
例如:
1. If given day is Monday through Friday, find the last closest Monday,
so if the given day is 10-31, I need to get 10-27
2. If given day is Saturday or Sunday, find the next Monday.
JavaScript:
var today = new Date(); //assuming it's 11/1/2014 Saturday
var todayDay = today.getDay(); > 6
if(todayDay == 6) {
var Monday = today.getDate() + 2;
}
我不知道如何动态地找到星期一的日期和时间。我已经查找了javascript day方法,但不知道如何获得它。有人能帮我吗?谢谢
因此,重新表述您的要求是:给定一周从周日开始,找到该周中的周一。
使用moment.js太容易了。。。
本周的周日
moment().startOf('week')
所以找到星期一做
moment().startOf('week').add('days', 1)
编辑
您可以使用以下时刻函数更改周初
moment.lang('en-in', {
week : {
dow : 1 // Monday is the first day of the week
}
});
而获取索引的星期几数组是
daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
javscript的Date对象具有方法getDay()、setDate()和getDate(),这些方法对此很有帮助。
"use strict";
var d = document.getElementById('d');
var mon = document.getElementById('mon');
var handleDatePick = function(ev){
var the_date = new Date(d.value);
if (the_date.getDay() <= 4) {
the_date.setDate( the_date.getDate() - the_date.getDay() );
} else {
the_date.setDate( the_date.getDate() + 7 - the_date.getDay() );
}
var output = 'the nearest monday is ' + the_date.toUTCString();
mon.value = output;
};
d.addEventListener('input',handleDatePick);
input, output {
display: block;
clear: both;
}
pick the date: <input type="date" id="d" />
<output id="mon" for="d"></output>
相关文章:
- 将日期时间javascript绑定到表列
- 为什么我的AngularJS ngModel绑定到时间输入,而不绑定到日期输入
- 如何使用 redux 时间旅行并将其绑定到 ctrl + z 和 ctrl + shift + z 键绑定
- 角度引导时间选取器不绑定时间
- 值属性的输入类型=“时间”绑定不会使用角度更新 UI
- 将两个函数绑定到一个按钮,具体取决于按钮的按下时间长短
- jquery 移动版的最佳绑定时间
- 错误绑定引导日期时间选取器到模型角度
- Angularjs ,如何设置输入的 ng 模型以通过自动绑定更改日期和时间
- 是否可以将日期/时间绑定到控制台日志
- 如何在给定时间内找到周一
- 如何让这个倒计时工作为周一,周二和其他时间
- Angularjs绑定事件的时间和位置
- Autocompleteextender控件在服务器上绑定数据库值花费了大量时间
- 用JavaScript将事件侦听器绑定到特定日期和时间之间的提交表单
- jQuery解除绑定事件的时间
- SAPUI5聚合绑定有时间延迟或呈现延迟吗?
- JavaScript绑定事件在特定时间
- 如何将backbone.js中的事件绑定延迟到某个时间之后
- AngularJS-文本不与Jquery时间选择器绑定