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, how
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.
you can try to use moment in UTC mode in this way:
let m = moment().utc()
then, to reset the time to midnight of that day you can call this method:
m.startOf('day')
you can also chain it all together:
let m = moment.utc().startOf('day')
and if you print it it will be something like:
console.log(m.format())
// 2019-07-02T00:00:00Z
You can also do it with the native Date, but you'll have to write a bunch of code that momentjs already has...
I hope this answer your questions!
for reference have a look here: https://momentjs.com/docs/#/parsing/utc/
and here: https://momentjs.com/docs/#/manipulating/start-of/