Steve
Steve

Reputation: 190

Angular datetime binding timezone conversion

I have an app that get's a datetime via a json object from a rest service. It comes back like this:

2014-03-30T08:00:00

I bind to it and pass it through the date filter, it looks like it puts it into local time.

{{ mytime.begin | date:'dd-MMMM-yyyy hh:mm a Z' }}
30-March-2014 08:00 AM -0400

But if I bind to it from a date time picker (every one I've tried so far), it subtracts the offset but still thinks it's utc. Here's an example from the angular-strap timepicker

<input type="text" size="8" class="form-control" ng-model="mytime.begin" time-format="hh:mm a Z" data-autoclose="1" placeholder="Time" bs-timepicker> 
04:00 AM -0400

But if I set the time through the timepicker it binds the other way correctly.

Thanks for any help.

Upvotes: 0

Views: 2236

Answers (1)

Steve
Steve

Reputation: 190

Ok so here is what I have discovered. You know what they say about assumptions ;). So MySQL is storing the dates as UTC, but does not bother to be specific about that with a trailing 'Z' or '+0000'. When that date is returned and parsed it is handled differently in different cases.

Angular Filter assumes ambiguous dates are in local time.

{{ mytime.begin | date:'dd-MMMM-yyyy hh:mm a Z' }}
30-March-2014 08:00 AM -0400

The javascript Date.parse function will assume ambiguous dates are in UTC. Angular Strap has it's own date parsing helper, I assume it's doing the same thing.

<input type="text" size="8" class="form-control" ng-model="mytime.begin" time-format="hh:mm a Z" data-autoclose="1" placeholder="Time" bs-timepicker> 
04:00 AM -0400

To add to the confusion, think my app server is doing the same thing but converting it to localtime. Anyway by being explicit about the time zone the issue goes away.

Upvotes: 2

Related Questions