In summation, calling locale on the global moment sets the locale for all future moment instances, but does not return an instance of moment. You can, however, change just the locale of a specific momentĬonsole.log(march.format('MMMM')) // 'Marzo' Moment.locale('de') // returns the new locale, in this case 'de'Ĭonsole.log(march.format('MMMM')) // 'March' still, since the instance was before the locale was setĬonsole.log(deMarch.format('MMMM')) // 'März' Some examples: var march = moment('2017-03')Ĭonsole.log(march.format('MMMM')) // 'March' It is hard to imagine writing JavaScript without it.You need moment.lang ( WARNING: lang() is deprecated since moment 2.8.0, use locale() instead): moment.lang("de").format('LLL') Īs of v2.8.1, moment.locale('de') sets the localization, but does not return a moment. The moment.format() function is so flexible, In particular, date formatting is usually the first reason Time zones, localization, addition and subtraction, durations: Moment makes mostĬomplex date tasks easy. Moment is an extremely powerful library with an unbelievable breadth of features. Prints "vor einer Minute" console.log(duration.humanize( true)) Moving On In your customer's language: const moment = require( 'moment') The humanize() function is locale aware, so you can render durations True, indicates whether the duration is positive or negative ('in a minute' vs 'a minute ago'). The humanize() function takes an optional parameter suffix that, if set to Moment.duration(m2.diff(m1)).humanize( true) // 'a minute ago' For example: const moment = require( 'moment') Ĭonst m1 = moment( new Date( '1 2:04:03')) Ĭonst m2 = m1.clone().add( 59, 'seconds') Ĭonst duration = moment.duration(m1.diff(m2)) ĭuration.humanize( true) // 'in a minute' The moment.diff() function returns a Moment duration object that represents the difference between two moments.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |