gpt4 book ai didi

javascript - 始终使用 UTC+0 - 使用 javascript/angularjs 修复客户端浏览器上的自定义时区

转载 作者:搜寻专家 更新时间:2023-11-01 04:36:02 25 4
gpt4 key购买 nike

如何使用 javascript 在客户端浏览器上修复自定义时区?

例如,在 Angular 上,我有一个来自后台的日期“2015-10-16T00:00:00.000Z”。

我想要一个显示器(UTC-4 纽约或 UTC+2 法国),总是:16/10/2015

阅读:

如果我在纽约使用 UTC,我有:15/10/2015。

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

写:

我将日期原型(prototype)修改为 JSON 以删除时区:

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

最佳答案

我添加了这个:

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

return this;
};

这在我的 Controller /模型上:

    new Date(myDate).addTimeZone();

恢复:

extend-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();

我使用 moment.js

关于javascript - 始终使用 UTC+0 - 使用 javascript/angularjs 修复客户端浏览器上的自定义时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33170813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com