breezejs: date is not set to the right time

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.

Leave a Comment