网站地图    收藏   

主页 > 前端 > javascript >

js中dijit.form.DateTextBox日期自动转化方法详解

来源:自学PHP网    时间:2014-09-19 14:47 作者: 阅读:

[导读] 本文章来给各位同学介绍关于js中dijit.form.DateTextBox日期自动转化方法详解,希望些方法对各位同学有所帮助。...

调查了一下,可以用下面的代码来解决。

如下代码:

 代码如下 复制代码

dojo.declare("net.yutuo.dojo.DateTextBox", [dijit.form.DateTextBox], {   
    validator: function(value, constraints) {
        value = this.myParse(value, constraints);
        return (new RegExp("^(?:" + this.regExpGen(constraints) + ")"+(this.required?"":"?")+"$")).test(value)
                && (!this.required || !this._isEmpty(value))
                && (this._isEmpty(value) || this.parse(value, constraints) !== undefined);
    },
    parse: function(value, constraints){
        value = this.myParse(value, constraints);
        return this.dateLocaleModule.parse(value, constraints)
                || (this._isEmpty(value) ? null : undefined);
    },
    myParse: function (value, constraints) {
        var dateStr = null;
        if (value.match(/^d{6}$/)) {
            dateStr = value.substring(0, 2) + '-' + value.substring(2, 4) + '-' + value.substring(4, 6);
        } else if (value.match(/^d{8}$/)) {
            dateStr = value.substring(0, 4) + '-' + value.substring(4, 6) + '-' + value.substring(6, 8);
        } else if (value.match(/^d{4}$/)) {
            dateStr = new Date().getFullYear() + '-' + value.substring(0, 2) + '-' + value.substring(2, 4);
        }
        if (dateStr) {
            var date = this.dateLocaleModule.parse(dateStr, {selector: 'date', datePattern: 'yy-M-d'});
            if (date) {
                return this.dateLocaleModule.format(date, constraints);
            }
        }
        return value;
    }
});

测试代码如下

 代码如下 复制代码

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" djConfig="parseOnLoad: true"
   src="js/dojo/dojo.js"></script>
<link rel="stylesheet" type="text/css"
   href="js/dojo/resources/dojo.css" />
<link rel="stylesheet" type="text/css"
   href="js/dijit/themes/claro/claro.css" />

<title>Dojo Test</title>

<script type="text/javascript">
    dojo.require("dijit.form.DateTextBox");
   
    dojo.declare("net.yutuo.dojo.DateTextBox", [dijit.form.DateTextBox], {   
        validator: function(value, constraints) {
            value = this.myParse(value, constraints);
            return (new RegExp("^(?:" + this.regExpGen(constraints) + ")"+(this.required?"":"?")+"$")).test(value)
                    && (!this.required || !this._isEmpty(value))
                   && (this._isEmpty(value) || this.parse(value, constraints) !== undefined);
        },
        parse: function(value, constraints){
            value = this.myParse(value, constraints);
            return this.dateLocaleModule.parse(value, constraints)
                    || (this._isEmpty(value) ? null : undefined);
        },
        myParse: function (value, constraints) {
            var dateStr = null;
            if (value.match(/^d{6}$/)) {
                dateStr = value.substring(0, 2) + '-' + value.substring(2, 4) + '-' + value.substring(4, 6);
            } else if (value.match(/^d{8}$/)) {
                dateStr = value.substring(0, 4) + '-' + value.substring(4, 6) + '-' + value.substring(6, 8);
            } else if (value.match(/^d{4}$/)) {
                dateStr = new Date().getFullYear() + '-' + value.substring(0, 2) + '-' + value.substring(2, 4);
            }
            if (dateStr) {
                var date = this.dateLocaleModule.parse(dateStr, {selector: 'date', datePattern: 'yy-M-d'});
                if (date) {
                    return this.dateLocaleModule.format(date, constraints);
                }
            }
            return value;
        }
    });

</script>
</head>

<body class="claro">
    Old:<br/>
    <input type="text" data-dojo-type="dijit.form.DateTextBox"/>

    <br/>   
    New:    <br/>
    <input type="text" data-dojo-type="net.yutuo.dojo.DateTextBox"/>
</body>

</html>

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论