Javascript 字符串到日期时间转换javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2605422/
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 01:07:04  来源:igfitidea点击:

string to datetime conversion javascript

javascript

提问by Dee

How do I convert string(09-Apr-2010) in to date time (09 April 2010 00:00:00) using java script? I need to compare the dates for validation.

如何使用 java 脚本将字符串(09-Apr-2010)转换为日期时间(2010 年 4 月 9 日 00:00:00)?我需要比较验证日期。

回答by user187291

check out Date.parse

查看Date.parse

str = "09-Apr-2010"
date = new Date(Date.parse(str.replace(/-/g, " ")))
alert(date.toLocaleString())

回答by Ravia

Try this should work

试试这个应该有效

<script language="javascript">
    function validateDate(oSrc, args)
    {
        var iDay, iMonth, iYear;
        var arrValues;
        arrValues = args.Value.split("/");
        iMonth = arrValues[0];
        iDay = arrValues[1];
        iYear = arrValues[2];

        var testDate = new Date(iYear, iMonth - 1, iDay);

        if ((testDate.getDate() != iDay) ||
            (testDate.getMonth() != iMonth - 1) ||
            (testDate.getFullYear() != iYear))
        {
            args.IsValid = false;
            return;
        }

        return true;
    }
</script>