@cxing/moment-extension v0.1.2
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-extensionUsage
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 itSupporting 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.
addsubtractstartOfendOfisSameisBeforeisAfterisSameOrBeforeisSameOrAfter
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()(centuryanddecadeare 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 ofmoments according to thestepandunit, defaulting to 1/day fromstarttoendmoment().inRange(start:moment, end:moment) => booleantells you if you're currentmomentfalls within a specific range
Calendar Convenience Methods
We also provide some basic convenience method for use with calendar scenarios.
moment().firstVisibleDay() => momentgets the first visible calendar day of themoment'smonthbased upon what thelocalegives for the first day of the week.moment().lastVisibleDay() => momentgets the last visible calendar day of themoment'smonthbased upon what thelocalegives for the first day of the week.moment().calendarDays([month:number = undefined]) => moment[]gets an array ofmoments representing the calendar days of themoment'smonth. If amonthis provided then it will return the days of that month, without mutating themoment.moment().calendarMonths([year:number = undefined]) => moment()gets an array ofmoments representing the first day of each month of themoment'syear. If ayearis provided then it will return the months of that year, without mutating themoment.moment().calendarDecade([year:number = undefined]) => moment[]gets an array ofmoments representing the first day of each year of themoment'sdecade. If ayearis 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 moments will have this information. We've also made it easier to set your timezone.
moment.setTimezone([zoneName|undefined])Ifundefinedis passed it will reset the default back to themoment.tz.guess(). If azoneNameis passed, and it is not a valid IANA timezone, it will throw an Error. If you have not includedmoment-timezonethen this will do nothing without errormoment.currentIANAZoneNameis a new property to access the current default IANA timezone name.
Once called, this ensures that all future moments 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|undefinedIt the givenvaluecan be strictly parsed using the suppliedformatthen it will return a moment. If it can not create a validmomentthen it will returnundefined.moment.validateInputValue(value:string, format:string, [maskChar:string = '_']) => moment|undefinedThis will safely strip theinputof anymaskChars, then trim that value, the run it throughisValidForFormat. If theinputis incomplete (strippedmaskChars) then it will fail 'strict'formatvalidation and returnundefined. If theinputis complete, but incorrect, it will also fail 'strict'formatvalidation.
Support Us
Did this help you? Help further our Open Source development and buy us a cup of coffee.