eight04 / angular-datetime

A directive to add the behavior of datetime input on unsupported browsers.
MIT License
93 stars 28 forks source link

UK Date Format #3

Closed Neilski closed 9 years ago

Neilski commented 9 years ago

I have a simple date only field using the UK data format dd/MM/yyyy as follows:

<input id="startDate" type="text" name="startDate" datetime="dd/MM/yyyy" ng-model="startDate" required />

If I then add a bound value to the page:

<p>Date: {{ startDate }}</p>

and set the input to '19/5/2015' the reflected value changes to '2015-05-18T23:00:00.000Z' which is one hour behind where I would have expected it to be.

I am not sure if this issue is related to daylight savings (we are currently running British Summer Time GMT+1)?

eight04 commented 9 years ago

'2015-05-18T23:00:00.000Z' is in ISO format.

The timezone is always zero UTC offset, as denoted by the suffix "Z"

To get locale time, try:

<p>Date: {{ startDate.toLocaleString() }}</p>

or use the date filter in Angular:

<p>Date: {{ startDate | date:'fullDate' }}</p>
Neilski commented 9 years ago

Thank you for the explanation, but I am still slightly confused... The date in question is being supplied from an HTTP GET request from the server. The date supplied by the server is '2015-05-15T00:00:00' and it is this date that initially gets bound and displayed in the view, in my case 15/5/2015. When the data gets POSTed back it gets changed to '2015-05-14T23:00:00Z' i.e. the displayed date -1 hour. It is this conversion from displayed date to bound date that is confusing me. In this scenario, is it expected that the server should always store the date with a zero UTC offset and then convert to to locale time to service the request or store in local time and convert the response?

Sorry, I suspect this is a more general question than it should be.

Neilski commented 9 years ago

Please ignore my last message. It seems that AngularJS's $http.post() method is responsible from the conversion from local date (UTC+ 1) to UTC+0. I will investigate further - thank you for your help.