breezejs: date is not set to the right time

前端 未结 2 918
感动是毒
感动是毒 2020-11-28 13:06

I\'ve noticed that if a date property comes back from the server with the value \"2013-07-11T17:11:04.700\", then breeze changes the value to Thu Jul 11 19:11:04 UTC+0200 20

2条回答
  •  没有蜡笔的小新
    2020-11-28 13:39

    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;
                        }
                    }
                }
    

提交回复
热议问题