breezejs: date is not set to the right time

让人想犯罪 __ 提交于 2019-11-26 22:46:45

Breeze does not manipulate the datetimes going to and from the server in any way EXCEPT to add a UTZ timezone specifier to any dates returned from the server that do not already have one. This is only done because different browsers interpret dates without a timezone specifier differently and we want consistency between browsers.

The source of your issues is likely to be that when you save your data with dates to the database, that the dateTime datatype you are using does NOT contain a timezone offset. This means that when the data is retrieved you are likely "losing" the offset and the Breeze default mentioned above kicks in. This can be corrected by using a database date time datatype with an timezone offset ( datetime2 or datetimeoffset in SQLServer).

Note that your browser DOES format dates according to it's current timezone.

Another approach is that you can replace Breeze's DataType.parseDateFromServer to NOT infer any time zone info if it is not provided:

breeze.DataType.parseDateFromServer = function (source) {
     return new Date(Date.parse(source));
};

However, this can run into the problem that different browsers interpret DateTime strings without a time zone offset differently... So you may still get strange results depending on the browser. If that happens you will need to add some browser detection code to the snippet above.

Another alternative is to do the following using the moment.js library.

breeze.DataType.parseDateFromServer = function (source) {
     var date = moment(source); 
     return date.toDate();   
};

Not sure how helpful this is, but hopefully Breeze's behavior is clearer.

Neha Jain

By default, Breeze does not provide any way to do this, but you can keep the below code in your model JS file to overcome this issue:

breeze.DataType.parseDateFromServer = function (source) {
                if (typeof source === 'string') {
                    //Check for local offset time or UTC time from server
                    if (source.slice(-1) !== "Z") {
                        var oldSource = source;

                        try {
                            source = source.substring(0, source.lastIndexOf("-") - 1)
                            source = new Date(source);
                            var tzDifference = source.getTimezoneOffset();
                            //convert the offset to milliseconds, add to targetTime, and make a new Date
                            var offsetTime = new Date(source.getTime() + tzDifference * 60 * 1000);
                            return offsetTime;
                        }
                        catch (err) {
                            source = new Date(source);
                            return source;
                        }
                    }
                    else {
                        source = new Date(source);

                        var tzDifference = source.getTimezoneOffset();
                        //convert the offset to milliseconds, add to targetTime, and make a new Date
                        var offsetTime = new Date(source.getTime() + tzDifference * 60 * 1000);

                        return offsetTime;
                    }
                }
            }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!