compare datetime currentdate javascript

compare datetime currentdate javascript

本文关键字:javascript currentdate datetime compare      更新时间:2023-09-26

我有txtStartDate控件,格式如下:2012年3月9日

我想比较txtStartDate值CurrentDate。如果txtStartDate大于CurrentDate,我希望返回true。如何做到这一点?

javascript中的日期很糟糕。我建议使用这个库FWIW,http://www.datejs.com/

具体来说http://code.google.com/p/datejs/wiki/APIDocumentation#compare

和相关

另请参见

http://code.google.com/p/datejs/wiki/APIDocumentation#parse用于实际将文本转换为日期对象。

反转日期:20120309,比较很容易。

function chgDate(dte){
    var pm=new String(dte.getMonth()+1);if(pm.length<2) pm='0'+pm;
    var pd=new String(dte.getDate());if(pd.length<2) pd='0'+pd;
    var py=dte.getFullYear();
    return new Number(py+pm+pd);
}
function chgControl(dte){
    dte=dte.split('.');
    return new Number(dte[2]+dte[1]+dte[0]);
}
bool=(chgControl('09.03.2012')>chgDate(new Date()));

您将希望从现有文本中创建一个Date对象。这可能涉及到获取字符串并将其拆分,然后将其传递给Date(year, month, day)构造函数。我不知道你的日子和月份是哪一轮,所以我不能给你示例代码。

创建Date对象后,可以将其与当前日期进行比较,可以使用new Date()获取当前日期。