Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(ANGULARJS) Input[time] is showing local time when given a regular date

Basically,

I received a date from my server which is, when printed out in the web console, this : Date 2016-05-04T09:00:00.000Z

Since, im using AngularJs, I did a binding using ng-model with an input time type.

Now the problem, is when ever I show the date in that input, I dont see: 9:00, but 5:00 (9:00 - 4:00) (I'm in a GMT -4 zone)

However the date isn't supposed to be a Universal Time, but as you can see it is parsed to local time. Any way to stop this?

like image 585
MasterJohn Avatar asked May 07 '16 18:05

MasterJohn


People also ask

What is input [time] in AngularJS?

input [time] is one of the AngularJS input directive in module ng. AnguarJS directive input [time] is used to create a standard HTML text input with time validation and transformation. In browsers does not yet support the HTML5 time input, instead text element will be used in a valid ISO-8601 local time format.

How to change the datetime format in angular?

To change the datetime format in angular we have to pass date time format parameter to the angular pipe as shown below. { { date_value | date :'short'}} // 6/15/19, 5:24 PM. The format ‘short’ is one of the predefined date formats in angular which converts our date value to ’M/d/yy, h:mm a’ format.

How to display date according to country locale format in angular?

To display date according to country locale format rules, We have to pass country locale code as a third parameter to angular date pipe as shown below. For example France follows Central European Summer Time and it has an timezone offset ‘+0200’.

What is date pipe in angular?

Angular date pipe used to format dates in angular according to the given date formats,timezone and country locale information. Using date pipe, we can convert a date object, a number (milliseconds from UTC) or an ISO date strings according to given predefined angular date formats or custom angular date formats.


1 Answers

The time zone to be used to read/write the Date instance in the model can be defined using ngModelOptions. By default, this is the timezone of the browser. AngularJS API input[time]

In your case the following option should do the trick.

<input type="time" ng-model-options="{ timezone: '-0400' }">

But beware with this option you are overwriting the browser's timezone. That means if a user operates in UTC+2 the input time is still treated as UTC-4.

like image 64
Paul Wasilewski Avatar answered Sep 27 '22 21:09

Paul Wasilewski