javascript - Dijit DateTextBox - 以 ISO/数字格式设置日期?

标签 javascript date dojo

我使用 DateTextBox 作为屏幕中的众多控件之一。我将它们注册在一处,然后在循环中批量设置它们的值,对每个值调用 set('value', val) 。所有控件都运行正常,只有 DateTextBox 不会接受来自服务器的数据。

最初java的日期被序列化为很长(例如1280959200000),但是当我更改为ISO格式(例如“2010-08-04T22:00:00.000+0000”)时,它也不被接受。但两者都是 new Date() 构造函数可接受的日期格式。

在输出中,我得到 ISO 格式的日期值:“2013-08-04T22:00:00.000Z”,因此输入也应该接受它。

如何使用 DateTextBox 使其接受 JavaScript 的 Date 对象支持的所有格式的值,或者可以从我的服务器返回的格式之一?

最佳答案

我认为根本问题是 Javascript 内置 Date 对象仅接受某些格式,而 Dojo 依赖于该内置行为。在工作中,我们遇到了类似的问题,许多遗留 PHP 代码习惯于以 Mysql 派生格式传递日期(例如 YYYY-MM-DD HH:MM:SS )

我们当前的解决方法是子类 dijit/form/DateTextBox ,这也让我们可以进行一些 UI 改进。当某些东西试图设置一个不是 Date 的值时对象并且看起来像 MySQL 日期时间,此代码重新形成它以匹配 ISO-8601 并将其传递。

DateTextBox 自定义版本的 Dojo 1.9 代码:

define([
    "dojo/_base/declare",
    "dojo/_base/lang",
    "dojo/_base/array",
    "dijit/form/DateTextBox"
], function(declare, lang, array, DateTextBox){

    var clazz = declare([DateTextBox], {


        _mysqlStyleExp : /^(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})$/,

        postMixInProperties: function(){ // change value string to Date object
            this.inherited(arguments);
            this.constraints.datePattern = "yyyy-MM-dd"; // Affects display to user
        },

        _convertMysqlDateToIso: function(value){
            value = lang.trim(value);
            var matches = this._mysqlStyleExp.exec(value);
            if(matches !== null){
                // Put the "T" in the middle and add fractional seconds with UTC
                // timezone

                // If your MySQL dates are NOT in UTC, obviously this will screw things up!                    
                return matches[1] + "T" + matches[2] + ".000Z";
            }else{
                return null;
            }
        },

        _setValueAttr : function(/*Date|String*/ value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
            /*
             We want to be slightly more permissive in terms of the strings that can be set in order to support
             older code... But someday it'd be nice to standardize on Date.toJSON, so warn.
             */
            if(typeof(value) === "string"){
                var isoDate = this._convertMysqlDateToIso(value);
                if(isoDate !== null){
                    console.warn("Converting non-ISO date of "+value);
                    value = isoDate;
                }
            }
            this.inherited(arguments);
        }        
    });

    return clazz;
});

请注意,这只会影响流入 Dojo 小部件的数据。

关于javascript - Dijit DateTextBox - 以 ISO/数字格式设置日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19514339/

相关文章:

javascript - 针对 Mobile Safari 内的文档调试外部 Javascript

javascript - 将 get 请求中的日期时间与 JavaScript 中的当前日期时间进行比较

php - 在 PHP 中给定周末和无工作日,选择具有非空值的最新日期

Python:如何从日期列表中计算日期范围?

javascript - 使用 dojo javascript 动态更改 div 的背景图像

javascript - angularjs指令使用 Controller 作为vm而不是作用域

javascript - 为什么 foreach 没有按预期工作?

c# - 将 MYSQL 日期获取到 C# 字符串异常中

jQuery UI 元素与 Dojo (Dijit) 表单元素

javascript - localStorage Size Limits...有哪些选项?