Javascript 在javascript中将字符串转换为日期时间并与当前时间进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27564639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:19:44  来源:igfitidea点击:

Convert string to datetime in javascript and compare with current time

javascriptjquerystring-to-datetime

提问by Mudassir Hasan

I am trying to pick up value from datetimepicker tetxbox and compare those values with current time.

我正在尝试从 datetimepicker tetxbox 中获取值并将这些值与当前时间进行比较。

JSFiddle

JSFiddle

        //startTime textbox text = 19/12/2014 03:58 PM
        var startTime = Date.parse($('[id$=txtStartDate]').val().toString());

        //endTime textbox text = 19/12/2014 04:58 PM
        var endTime = Date.parse($('[id$=txtEndDate]').val().toString());

        var currentTime = Date.now();
        alert(startTime);
        alert(endTime);
        alert(currentTime);

        if (currentTime >= startTime && currentTime <= endTime) {
              alert();

        }

Date.parse() is used fro converting string to milliseconds since Jan 1 1970. Date.now() returns current date milliseconds since Jan 1 1970.

Date.parse() 用于将字符串转换为自 1970 年 1 月 1 日以来的毫秒数。 Date.now() 返回自 1970 年 1 月 1 日以来的当前日期毫秒数。

But the above conversion methods are not working properly. What should be logic to compare datetime by first sonverting string in format like 19/12/2014 03:58 PMto Date object and then do comparing.

但是上面的转换方法都不能正常工作。通过首先将格式为19/12/2014 03:58 PM 的字符串与 Date 对象进行比较,然后进行比较,应该是什么逻辑。

回答by T.J. Crowder

Since that format isn't documented as being supported by Date.parse, your best bet is to parse it yourself, which isn't difficult: Use String#splitor a regular expression with capture groups to split it into the individual parts, use parseIntto convert the parts that are numeric strings into numbers (or, with controlled input like this, just use the unary +on them), and then use new Date(...)to use those numbers to create a Dateinstance.

由于该格式未记录为受 支持Date.parse,因此最好的办法是自己解析它,这并不困难:使用String#split或带有捕获组的正则表达式将其拆分为各个部分,用于parseInt转换部分数字字符串转换为数字(或者,对于这样的受控输入,只需+对它们使用一元),然后new Date(...)使用这些数字来创建Date实例。

One gotcha: The monthvalue that new Dateexpects is zero-based, e.g. 0 = January. Also remember to add 12 to the hours value if the input uses AM/PM instead of the 24-hour clock.

一个问题:期望的monthnew Date从零开始的,例如 0 = 一月。如果输入使用 AM/PM 而不是 24 小时制,还要记住将 12 添加到小时值。



Or, of course, use any of several date/time handling libraries, such as MomentJS.

或者,当然,使用多个日期/时间处理库中的任何一个,例如MomentJS

回答by Johan Karlsson

The problem is Date()expects date format mm/dd/yyyy, so your date is invalid.

问题是Date()期望日期格式为 mm/dd/yyyy,因此您的日期无效。

You can fix your date like this:

您可以像这样修复您的日期:

function toValidDate(datestring){
    return datestring.replace(/(\d{2})(\/)(\d{2})/, "");   
}

var startTime = Date.parse(toValidDate($('[id$=txtStartDate]').val().toString())); 
var endTime = Date.parse(toValidDate($('[id$=txtEndDate]').val().toString()));    
var currentTime = Date.now();

alert(startTime);
alert(endTime);
alert(currentTime);

DEMO: http://jsfiddle.net/3mztdaja/3/

演示:http: //jsfiddle.net/3mztdaja/3/

回答by Abdallah Arffak

You should use this method

你应该使用这个方法

var startTime  = new Date(year, month, day, hours, minutes, seconds, milliseconds);

this a demo http://jsfiddle.net/hswp7x8k/

这是一个演示http://jsfiddle.net/hswp7x8k/

to extrat value from string you can use this method

从字符串中提取值,您可以使用此方法

dd = '19/12/2014 03:58';
dd.match(/(\d+)\/(\d+)\/(\d+)\s*(\d+):(\d+)/);

this a demo http://jsfiddle.net/w3wow1ay/2/

这是一个演示http://jsfiddle.net/w3wow1ay/2/