Twix.js allows you to work with date ranges
Twix is a simple but opinionated JS library for working with date ranges, and includes a customizable smart formatter. It's written in CoffeeScript and it depends on Moment.js.
It allows you to do, among other things, this:
var t = moment"1982-01-25T09:30"twix"1982-01-25T13:30";tformat; //=> Jan 25, 1982, 9:30 AM - 1:30 PMtisSame"day"; //=> truethumanizeLength; //=> "4 hours"tcount"days"; //=> 1
And much more.
Attention Moment 2.10.x users: You need to upgrade to the latest Twix! You also should make sure not to be on Moment 2.10.0 (i.e. upgrade to the latest point release) because it has some compatibility issues with Twix.
Breaking change in 0.7.0: Twix French internationalization (for French) is no longer supported. It doesn't seem to have had a lot of use, and it's pain to maintain.
If you want to build Twix for yourself, clone the repo and run this:
make configure build
Configure just installs the NPMs (including CoffeeScript) and brings in Moment as a submodule, so you only have to do that part once.
Note that the source is
src/twix.coffee; the output is
dist/twix.js. You can run the tests via
You can also run the tests in-browser by building and then loading
Patches are welcome!
developbranch. I merge develop to master when we cut a release.
.jsfiles in the patch; they're much harder to merge. I'll generate them when I cut the release.
0.7.0: Fixed bug in
difference(). Fixed compatibility with other plugins. Removed custom locale support. Deprecated
0.6.5: Fix for
contains() on all-day ranges
0.6.4: Use peerDependency for Moment
0.6.3: Moment 2.10 compatibility
iterate for months
0.6.1: Meteor support
0.6.0: Moment deprecations, s/lang/locale, xor, intersection, split, difference, immutability
0.5.1: Make Twix compatible with Moment 2.8.1
0.5.0: Fix intersection/overlap behavior (#36), expand signature, and deal with Moment deprecations.
0.4.0: Simplified internationalization support (possibly breaking change for those few people using French support), added more complex iteration durations. Reverted build complexity.
0.3.0: AMD support, basic internationalization support, bug fix for
iterateInner. Component and bower support. New build system and code organization.
overlaps for adjacent times, renamed
union, added deprecation warnings.
isCurrent. Added duration methods. Emphasized moment() monkey patch methods over Twix() constructor. Some bug fixes.
Older versions - wasn't tracking.
Copyright (c) 2012 Isaac Cambron
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.