简体   繁体   English

如何检查日期时间是否在范围之间? jQuery或javascript中的YYYY-MM-DD HH:MM:SS格式

[英]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;
}

FIDDLE 小提琴


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.

相关问题 如何使用 Javascript 或 jQuery 从 YYYY-MM-DD HH:MM:SS 修剪 HH:MM:SS - How to trim HH:MM:SS from YYYY-MM-DD HH:MM:SS using Javascript or jQuery 如何使用JavaScript将数据格式“ YYYY-mm-dd hh:mm:ss”转换为“ dd-mm-YYYY hh:mm:ss”? - How to convert data format “YYYY-mm-dd hh:mm:ss” to “dd-mm-YYYY hh:mm:ss” using javascript? 如何检查字符串是否为有效日期格式(格式应为:YYYY-MM-DD HH:mm:ss) - how to check the string is valid date format or not (format should be in : YYYY-MM-DD HH:mm:ss) 如何在 javascript 中解析 yyyy-MM-dd HH:mm:ss.SSS 格式的日期? - How to parse yyyy-MM-dd HH:mm:ss.SSS format date in javascript? Javascript'yyyy-mm-dd hh:mm:ss'到时间戳 - Javascript 'yyyy-mm-dd hh:mm:ss' to timestamp 日期时间正则表达式模式匹配 yyyy-MM-dd hh:mm:ss 格式 - datetime Regular Expressions pattern to match yyyy-MM-dd hh:mm:ss format 如何在核心 JavaScript 中将新日期 object 格式化为 MySQL 格式 YYYY-MM-DD hh-mm-ss? - How to format new date object to MySQL format YYYY-MM-DD hh-mm-ss in core JavaScript? 将YYYY-MM-DD HH:MM:SS转换为Javascript中的其他格式 - Convert YYYY-MM-DD HH:MM:SS to different format in Javascript 将毫秒转换为 yyyy-MM-dd HH:mm:ss 格式 Javascript - Convert Milliseconds to yyyy-MM-dd HH:mm:ss format Javascript javascript格式日期obj到yyyy-MM-dd hh:mm:ss返回NAN-NAN-NAN - javascript format Date obj to yyyy-MM-dd hh:mm:ss returns NAN-NAN-NAN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM