breezejs:日期没有设置在正确的时间 [英] breezejs: date is not set to the right time

查看:102
本文介绍了breezejs:日期没有设置在正确的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,如果某个date属性从服务器返回值为2013-07-11T17:11:04.700,则微风更改为Thu Jul 11​​ 19:11:04 UTC + 0200 2013 。



注意时间现在是2小时前!



在保存实体时,我已经遇到这个问题,所以我不得不使用momentjs显式转换我的日期属性:



date.hours(date.hours() - moment()。zone()/ 60);

  

但现在看来,读取操作时也会出现问题。



确保微风不会改变我的日期属性的值的最佳方式是什么?

解决方案

Breeze 不会以任何方式操纵到服务器的数据时间 EXCEPT添加从服务器返回的任何日期的UTZ时区说明符,该日期还没有。这只是因为不同的浏览器解释日期而不使用时区说明符不同,我们希望浏览器之间的一致性。



您的问题的根源可能是当您保存数据与日期到数据库,您使用的dateTime数据类型不包含时区偏移量。这意味着当检索到数据时,您可能会丢失偏移量,并且上面提到的Breeze默认值可以通过使用具有时区偏移(SQLServer中的datetime2或datetimeoffset)的数据库日期时间数据类型进行更正。 p>

请注意,您的浏览器按照当前时区的格式日期。另一种方法是,如果没有提供,可以将Breeze的DataType.parseDateFromServer替换为不推断任何时区信息:

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

但是,这可能会遇到不同浏览器解释日期时间字符串而不使用时区偏移的问题。 ..所以你可能会得到奇怪的结果,取决于浏览器。如果发生这种情况,您将需要在上面的代码段中添加一些浏览器检测代码。



另一个替代方法是使用moment.js库进行以下操作。

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

不知道这是多么有用,但希望Breeze的行为更清晰。


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 2013.

Notice the time is now 2 hours ahead !

I had already come across this issue when saving entities, so I had to explicitly convert my date properties using momentjs :

 date.hours(date.hours() - moment().zone() / 60);

But now it seems the problem occurs also when doing read operations.

What's the best way to make sure breeze does not alter values of my date properties ?

解决方案

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.

这篇关于breezejs:日期没有设置在正确的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆