jsmreese / moment-duration-format

Format function plugin for the Moment Duration object.
MIT License
968 stars 120 forks source link

momentWithLocales2.default.duration(...).format is not a function #102

Closed VanessaChu closed 6 years ago

VanessaChu commented 6 years ago

I'm using React Native and I'm trying to change the language of my moment.js by changing import moment from 'moment' to import moment from 'moment/min/moment-with-locales'.

import moment from 'moment/min/moment-with-locales' import 'moment-duration-format' // used by moment

However, I get an error "momentWithLocales2.default.duration(...).format is not a function" at this line:

const total = moment.duration(this.props.stoveUsage.total, 'seconds').format('H:mm', { trim: false })

before there was no error with just import moment from 'moment'

jsmreese commented 6 years ago

If moment.duration.fn.format is not a function, it means that when the moment-duration-format plugin was imported, it couldn't figure out what moment instance to use. The plugin module exports its setup function, so you'll need to do something like this (I'm not totally sure how it should look with the import style of including a module):

var newMomentInstance = import moment from 'moment/min/moment-with-locales';
var momentDurationFormatSetup = import 'moment-duration-format';
momentDurationFormatSetup(newMomentInstance);
jsmreese commented 6 years ago

@VanessaChu let me know if you haven't resolved your issue. Closing this for now.