[英]How to check datetime if it falls between a range? YYYY-MM-DD HH:MM:SS format in jquery or javascript
GIVEN: 已知:
start range: 2016-01-01 21:00:00
end range: 2016-02-29 23:59:59
inputed time: 2016-02-03 01:00:00
I cannot find any code that checks YYYY-MM-DD HH:MM:SS format at once. 我找不到任何可一次检查YYYY-MM-DD HH:MM:SS格式的代码。 I only find codes for checking either the date or time.
我只找到用于检查日期或时间的代码。
so I just combined both and did something like this. 所以我将两者结合在一起,做了这样的事情。
pseudo code: 伪代码:
if(checkdate()== true)
{
if(checktime()== true)
{
alert('yes');
}
else
{
alert('no');
}
}
else
{
alert('no');
}
I just realized that doesn't work after all because based on my Given example. 我只是意识到这根本行不通,因为基于我的给定示例。
It would fall true for checkdate() but be false for checktime() since 01:00:00 does not fall between 21:00:00-23:59:59 but it should have been true because the start and end ranges are different days. 对于checkdate()来说它是正确的,但对于checktime()来说它是错误的,因为01:00:00不在21:00:00-23:59:59之间,但是它应该是正确的,因为开始和结束范围是不同的天。
How do i do this? 我该怎么做呢?
Create a function checkdate
that parses the dates and compares 创建一个函数
checkdate
来解析日期并进行比较
function checkdate(_input, _start, _end) {
function parseDate(x) {
if (typeof x === 'string') {
var parts = x.split(/[-: ]/g).map(Number);
x = new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]);
}
return x;
}
_input = parseDate(_input);
_start = parseDate(_start);
_end = parseDate(_end);
return _input > _start && _input < _end;
}
For fun only, here's a golfed version 仅出于娱乐目的,这是高尔夫球版
function checkdate() {
var y = [].slice.call(arguments).map(function(x) {
var arr = [null].concat(x.split(/[-: ]/g).map(Number)); arr[2]--;
return new (Function.prototype.bind.apply(Date, arr));
});
return y[0] > y[1] && y[0] < y[2];
}
Something like 就像是
var startRange = (new Date("2016-01-01 21:00:00".replace(/-/g, '/')).getTime()),
endRange = (new Date("2016-02-29 23:59:59".replace(/-/g, '/')).getTime()),
input = (new Date("2016-02-03 01:00:00".replace(/-/g, '/')).getTime());
if(input >= startRage && input <= endRange) {}
You can compare Date objects directly. 您可以直接比较Date对象。 Convert your strings to correctly formatted date strings if needed.
如果需要,将您的字符串转换为正确格式的日期字符串。
var beginRange = new Date(".....");
var endRange = new Date(".....");
var myDate = new Date(".....");
if (myDate > beginRange && myDate < endRange)
...
Below is the working solution. 以下是有效的解决方案。 You need to replace "-" with "/" then convert to Date
您需要将“-”替换为“ /”,然后转换为日期
var startDate = new Date("2016-01-01 21:00:00".replace(/-/g, '/'));
var endDate = new Date("2016-02-29 23:59:59".replace(/-/g, '/'));
var input = new Date("2016-02-03 01:00:00".replace(/-/g, '/'));
if(input <= endDate && input >= startDate) {
alert("success");
}
else {
alert("Failure");
}
Consider using regex: 考虑使用正则表达式:
var date = "2016-01-01 21:00:00";
var pattern = /\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2}/;
var correct_match = date.match(pattern);
if (correct_match) {
alert('yes');
}
else {
alert('no');
}
Since you're using a sensible datetime format, you can just use string comparison: 由于您使用的是明智的日期时间格式,因此可以只使用字符串比较:
$ node
> rangeStart="2016-01-01 21:00:00"
'2016-01-01 21:00:00'
> rangeEnd="2016-02-29 23:59:59"
'2016-02-29 23:59:59'
> inputDate="2016-02-03 01:00:00"
'2016-02-03 01:00:00'
> if (rangeStart <= inputDate && inputDate <= rangeEnd) console.log("input date is in range")
input date is in range
You just need to validate that the input is in the right format. 您只需要验证输入的格式正确即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.