默认情况下,MomentJS/Date对象UTC [英] MomentJS/Date object UTC by default
问题描述
我在我的角度项目中使用MomentJS,但是在不同日期时区存在很多问题.
我的应用程序不应该考虑任何时区,但是在后端api和前端之间的这种混乱,当我将标准矩对象发送到c#后端时,默认情况下会将其转换为UTC,并且我总是得到-1天的日期./p>
我可以将MomentJS或javascript Date对象设置为始终默认为UTC并忽略小时/分钟/秒吗?
当我这样做时:moment()
它会根据时区和当前小时填充今天的值.
我想使moment()
始终具有UTC值,并且在整个应用中时间始终为00:00:00.
是否也可以通过new Date()
实现这一目标?
现在,我一直在new Date()
之后一直使用.toUTCString()
,但是我的解决方案中有很多日期变量,要确保所有内容在所有时间之后都具有.toUTCString()
并不是很确定开发人员错过了它会导致问题.我正在尝试找到一种在项目级别对此进行标准化的方法.
我的主要问题是momentJS比较方法.isBefore()
,.isSame()
,.isAfter()
.因为当比较发生时,我所有的日期都包含不同的时间和不同的时区,所以它将考虑到它们,并且不符合我希望它们的行为.
您可以使用瞬间时区与moment
.
然后使用以下代码设置默认的UTC
时区:
import * as moment from 'moment-timezone';
...
moment.tz.setDefault('Etc/UTC');
Angular中的一个可能选择是创建一个单例实例LocaleService
来管理与本地化相关的参数.
import { Injectable } from '@angular/core';
import * as moment from 'moment-timezone';
@Injectable({
providedIn: 'root'
})
export class LocaleService {
constructor() {
this.setDefaultTimezone();
}
setDefaultTimezone() {
moment.tz.setDefault('Etc/UTC');
}
// other stuff related to localization
...setCurrentTimezone()
...setLocale()
}
然后,我们需要通过AppModule
提供此服务,并且也不要忘记将其注入到全局组件(例如AppComponent
)中以便被实例化.
I am using MomentJS in my angular project and i'm having a lot of issues with different date timezones.
My app should not take into consideration any timezones, however this chaos between my backend api and frontend, when i send standard moment objects to c# backend it converts it to UTC by default and i always get the dates -1 day.
Can i set MomentJS or javascript Date object to always default to UTC AND ignore hour/minutes/seconds ?
When i do: moment()
it gets populated with today value based on timezone and based on current hour.
I want to make moment()
always have UTC value and time always be 00:00:00 in my entire app.
Is it also possible to achieve this also with new Date()
?
Right now i've been always using .toUTCString()
after new Date()
but i have a ton of date variables in my solution and it's not really ok to make sure everything has .toUTCString()
after it all the time, if another dev misses this it will cause issue. I'm trying to find a way to standardize this at project level.
My major issue is with momentJS compare methods .isBefore()
, .isSame()
, .isAfter()
. Because all my dates contain different hours and different time zone when the compare happens it will take them into account and not behave how i want them to.
You can use moment-timezone with moment
.
And then set default UTC
timezone with code below:
import * as moment from 'moment-timezone';
...
moment.tz.setDefault('Etc/UTC');
A possible option in Angular could be to create a singleton instance LocaleService
to manage localization related parameters.
import { Injectable } from '@angular/core';
import * as moment from 'moment-timezone';
@Injectable({
providedIn: 'root'
})
export class LocaleService {
constructor() {
this.setDefaultTimezone();
}
setDefaultTimezone() {
moment.tz.setDefault('Etc/UTC');
}
// other stuff related to localization
...setCurrentTimezone()
...setLocale()
}
And then, we need to provide this service via AppModule
, and also don't forget to inject it in a global component, for instance AppComponent
, in order to be instantiated.
这篇关于默认情况下,MomentJS/Date对象UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!