Always use UTC + 0 - Fix user time zone in client browser using javascript / angularjs

How can I fix custom timezone in client browser using javascript?

For example, on angular, I have the date "2015-10-16T00: 00: 00.000.000" from backoffice.

I would like to have a display (with UTC-4 in New York or with UTC + 2 France), always: 10/16/2015

To read:

If I use UTC in New York, I have: 10/15/2015.

<p ng-bind="(myDate | date:'dd/MM/yyyy')"></p> 

Record:

I changed the date prototype to JSON to remove the timezone:

 // Remove TimeZone Date.prototype.toJSON = function(){ return moment(this).format('YYYY-MM-DD') + 'T00:00:00.000Z'; }; 
+5
source share
2 answers

I added the following:

 // Add timeZone Date.prototype.addTimeZone = function () { if(this.getTimezoneOffset() > 0){ this.setMinutes(this.getTimezoneOffset()); } return this; }; 

And this is on my controller / model:

  new Date(myDate).addTimeZone(); 

To resume:

expanding-date prototype.js

 // Add timeZone Date.prototype.addTimeZone = function () { if(this.getTimezoneOffset() > 0){ this.setMinutes(this.getTimezoneOffset()); } return this; }; // Remove TimeZone Date.prototype.toJSON = function(){ return moment(this).format('YYYY-MM-DD') + 'T00:00:00.000Z'; }; 

view.html

 <p ng-bind="(myDate | date:'dd/MM/yyyy')"></p> 

controller.js

 new Date(myDate).addTimeZone(); 

I am using moment.js

+1
source

You can use getTimezoneOffset to get the client offset, and then simple math

 var dateToTranslate = new Date("2015-10-16T00:00:00.000Z"); var timezoneOffset = dateToTranslate.getTimezoneOffset() * 60000; var newDate = new Date(+dateToTranslate+timezoneOffset); // Fri Oct 16 2015 00:00:00 GMT+0200 (CEST) in my browser 
0
source

Source: https://habr.com/ru/post/1233866/


All Articles