带有日历的js中的日期比较 [英] Date comparision in js with globalized calender

查看:73
本文介绍了带有日历的js中的日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hu
我想在Java脚本中比较两个日期.

hu
i want to compare two dates in java script.

if uk
var dt=new Date("MM/dd/yyyy");
var dt2=new Date("MM/dd/yyyy");

if browser lang french
var dt=new Date("dd/mm/yyyy");
var dt2=new Date("dd/mm/yyyy");


如果是西班牙文...等

我如何比较两个独立于watsoever的日期,客户端可能会在其浏览器中使用区域性.


if spanish....etc

How can i compare two dates independent of watsoever client may use culture in his browser.

推荐答案

我将亲自确保您手动输入的日期输入格式清晰,我更喜欢DD-MMM-YYYY,消除了所有歧义.另外,如果无法识别日期,请处理此错误(下面的代码将写出任何无效日期").

首先要做的是解析日期对象中的日期.

然后只需对date对象的valueOf执行相等性测试(根据javascript docs,valueOf的输出与toString相同).

I would personally ensure your date entry for manual entries is clear in the format expected, I prefer DD-MMM-YYYY, removes all ambiguity. Also, if the date is not recognised handle this error (the code below will write out any "invalid date").

The first thing to do, is parse the dates in a date object.

Then simply perform an equality test of the valueOf the date object (according to the javascript docs, valueOf output is the same as toString).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>
    Date Input 1;<br />
    <input id="Text1" type="text" /><br />
    Date Input 2;<br />
    <input id="Text2" type="text" /><br />
    <input id="Button1" type="button" value="Compare Dates" onclick="compareDates()" />
    
<div id="output">Enter the dates and hit the button.</div>
<script type="text/javascript">

    function compareDates() {
        var d1 = new Date(Date.parse(Text1.value));
        var d2 = new Date(Date.parse(Text2.value));

        output.innerHTML = "D1 value is: " + d1.toUTCString() + "<br />" + "D2 value is: " + d2.toUTCString();

        alert("Are the dates the same; " + (d1.valueOf() == d2.valueOf()));

    }

</script>

</body>
</html>


这篇关于带有日历的js中的日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆