I'm using the webpack template from vue-cli for my Vue project and I have both moment and moment-timezone installed in npm.
In my single file components, I've been able to get moment to work with moment-timezone by including the following import lines:
import moment from 'moment'
import moment_timezone from 'moment-timezone'
The moment_timezone object isn't ever used, but it seems to need to be there for moment.tz functions to work.
My questions are:
Moment-timezone is a separate npm module from moment, but moment-timezone depends on moment under the hood. So you can install moment-timezone by itself, or both moment and moment-timezone. Once you install moment-timezone, you can require() it in and use it like you would use moment.
var tz = moment. tz. guess(); It will return an IANA time zone identifier, such as America/Los_Angeles for the US Pacific time zone.
Use Typescript @types packages and import it via import * as moment from 'moment-timezone'; You can use all moment methods and member vars as moment-timezone exports them. Show activity on this post. Show activity on this post.
This format is deprecated and will be removed in future.
Answering my own question as I seem to have sorted it out.
1 - It seems that moment-timezone requires the use of moment, so if you are going to use both moment and moment-timezone, you can simply import moment from the moment-timezone module and it will work just fine. So...
import moment from 'moment'
import moment_timezone from 'moment-timezone'
becomes
import moment from 'moment-timezone'
2 - Moment-timezone has files that represent the complete moment-timezone code plus specific versions of the data under data/builds. You can import moment-timezone directly from these files without needing to import the moment-timezone.js file in the root of the module. So...
import moment from 'moment-timezone'
becomes
import moment from 'moment-timezone/data/builds/moment-timezone-with-data-2012-2022.js'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With