@cxing/moment-extension
v0.1.2
Published
Extending MomentJs with additional support and functionality
Downloads
36
Maintainers
Readme
moment-extension
This is a decorator for MomentJs, adding simple math support for 'decade' and 'century', method aliases for comparison methods, simple range methods, and more.
It is written for modern browsers, and does not include CommonJs support.
There are no built-in dependencies to this project. As a decorator, it requires you to give it moment
, even if previously decorated in some way.
Install
npm install @cxing/moment-extension
Usage
myMoment.js
import moment from 'moment';
import 'moment-timezone'; // optionally
import decorate from '@cxing/moment-extension';
export default decorate(moment);
myPage.js
import moment from './myMoment.js';
// Do something with it
Supporting decade
and century
These simple math and comparison methods have been overridden to support decade
and century
as a date/time unit
without changing their standard method signatures.
add
subtract
startOf
endOf
isSame
isBefore
isAfter
isSameOrBefore
isSameOrAfter
Comparison Method Aliases
We've also provided aliases for all of the comparison methods, as well as one new one.
isSame()
->eq()
isBefore()
->lt()
isSameOrBefore()
->lte()
isAfter()
->gt()
isSameOrAfter()
->gte()
- New
neq()
(century
anddecade
are also supported)
Simple Range Methods
We provided some simple Range Methods
moment.range({ start:moment, end:moment, [unit:string = 'day'], [step:number = 1] }) => moment[]
returns an array ofmoment
s according to thestep
andunit
, defaulting to 1/day fromstart
toend
moment().inRange(start:moment, end:moment) => boolean
tells you if you're currentmoment
falls within a specific range
Calendar Convenience Methods
We also provide some basic convenience method for use with calendar scenarios.
moment().firstVisibleDay() => moment
gets the first visible calendar day of themoment
'smonth
based upon what thelocale
gives for the first day of the week.moment().lastVisibleDay() => moment
gets the last visible calendar day of themoment
'smonth
based upon what thelocale
gives for the first day of the week.moment().calendarDays([month:number = undefined]) => moment[]
gets an array ofmoment
s representing the calendar days of themoment
'smonth
. If amonth
is provided then it will return the days of that month, without mutating themoment
.moment().calendarMonths([year:number = undefined]) => moment()
gets an array ofmoment
s representing the first day of each month of themoment
'syear
. If ayear
is provided then it will return the months of that year, without mutating themoment
.moment().calendarDecade([year:number = undefined]) => moment[]
gets an array ofmoment
s representing the first day of each year of themoment
'sdecade
. If ayear
is provided then it will return the years of the corresponding decade, without mutating themoment
.
Timezone Convenience Method
These are only relevant if you're using moment-timezone. A moment
object doesn't contain zone
information unless it was either created with moment.tz()
or you've moment.tz.setDefault(someZone)
. The decorator will immediately setDefault(moment.tz.guess)
, so that all moment
s will have this information. We've also made it easier to set your timezone.
moment.setTimezone([zoneName|undefined])
Ifundefined
is passed it will reset the default back to themoment.tz.guess()
. If azoneName
is passed, and it is not a valid IANA timezone, it will throw an Error. If you have not includedmoment-timezone
then this will do nothing without errormoment.currentIANAZoneName
is a new property to access the current default IANA timezone name.
Once called, this ensures that all future moment
s are created using the supplied timezone. At an individual level this can still be overridden by using the moment.tz(value, zoneName)
syntax.
Validation Convenience Methods
Some developers use moment
for building out calendar or date/time picker controls. These convenience methods are provided to do 'strict' validation of text input against a format
.
moment.isValidForFormat(value:string, format:string) => moment|undefined
It the givenvalue
can be strictly parsed using the suppliedformat
then it will return a moment. If it can not create a validmoment
then it will returnundefined
.moment.validateInputValue(value:string, format:string, [maskChar:string = '_']) => moment|undefined
This will safely strip theinput
of anymaskChar
s, then trim that value, the run it throughisValidForFormat
. If theinput
is incomplete (strippedmaskChar
s) then it will fail 'strict'format
validation and returnundefined
. If theinput
is complete, but incorrect, it will also fail 'strict'format
validation.
Support Us
Did this help you? Help further our Open Source development and buy us a cup of coffee.