Created
May 17, 2015 15:39
-
-
Save lorenzoongithub/8b87b403af4a5242872e to your computer and use it in GitHub Desktop.
moment.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// moment.js is a powerful library to parse, validate, manipulate, and display dates in JavaScript. | |
// see: http://momentjs.com/ | |
// | |
// this gist was loosely based on this article | |
// http://www.sitepoint.com/managing-dates-times-using-moment-js/ | |
// | |
load('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.js'); | |
// | |
// validating dates in a given format | |
// | |
if (moment('12-12-2015','DD-MM-YYYY').isValid() == false) throw ''; | |
if (moment('29-02-2012','DD-MM-YYYY').isValid() == false) throw ''; | |
if (moment('29-02-2008','DD-MM-YYYY').isValid() == false) throw ''; | |
if (moment('29-02-2009','DD-MM-YYYY').isValid()) throw ''; | |
if (moment('29-02-2010','DD-MM-YYYY').isValid()) throw ''; | |
if (moment('30-02-2012','DD-MM-YYYY').isValid()) throw ''; | |
if (moment('31-06-2012','DD-MM-YYYY').isValid()) throw ''; | |
if (moment('32-05-2012','DD-MM-YYYY').isValid()) throw ''; | |
if (moment('05-2012','DD-MM-YYYY').isValid()) throw ''; | |
if (moment('xx-05-2012','DD-MM-YYYY').isValid()) throw ''; | |
// | |
// ISO 8601 | |
// | |
if (moment('2013-02-08').isValid()==false) throw '' // A calendar date part | |
if (moment('2013-W06-5').isValid()==false) throw '' // A week date part | |
if (moment('2013-039').isValid()==false) throw '' // An ordinal date part | |
if (moment('2013-02-08T09').isValid()==false) throw '' // An hour time part separated by a T | |
if (moment('2013-02-08 09').isValid()==false) throw '' // An hour time part separated by a space | |
if (moment('2013-02-08 09:30').isValid()==false) throw '' // An hour and minute time part | |
if (moment('2013-02-08 09:30:26').isValid()==false) throw '' // An hour | |
if (moment('2013-02-08 09:30:26.123').isValid()==false) throw '' // An hour | |
if (moment('2013-02-08 24:00:00.000').isValid()==false) throw '' // hour 24 | |
if (moment('2013-02-08 09').isValid()==false) throw '' // A calendar date part and hour time part | |
if (moment('2013-W06-5 09').isValid()==false) throw '' // A week date part and hour time part | |
if (moment('2013-039 09').isValid()==false) throw '' // An ordinal date part and hour time part | |
if (moment('2013-02-08 09+07:00').isValid()==false) throw '' // +-HH:mm | |
if (moment('2013-02-08 09-0100').isValid()==false) throw '' // +-HHmm | |
if (moment('2013-02-08 09Z').isValid()==false) throw '' // Z | |
if (moment('2013-02-08 09:30:26.123+07:00').isValid()==false) throw '' // +-HH:mm | |
// | |
// addition and subtraction | |
// | |
if (moment('2015-12-12').add(7,'days').isSame(moment('2015-12-19')) == false) throw ''; | |
if (moment('2015-12-12').add(7,'months').isSame(moment('2016-07-12')) == false) throw ''; | |
if (moment('2015-12-12').add(7,'years').isSame(moment('2022-12-12')) == false) throw ''; | |
if (moment('2015-12-12').subtract(7,'days').isSame(moment('2015-12-05')) == false) throw ''; | |
if (moment('2015-12-12').subtract(7,'months').isSame(moment('2015-05-12')) == false) throw ''; | |
if (moment('2015-12-12').subtract(7,'years').isSame(moment('2008-12-12')) == false) throw ''; | |
// | |
// Differences | |
// | |
if (moment('2014-11-11').diff(moment('2014-10-11')) != 2682000000) throw ''; | |
if (moment('2014-11-11').diff(moment('2014-10-11'),'days') != 31) throw ''; | |
if (moment('2014-11-11').diff(moment('2014-10-11'),'months') != 1) throw ''; | |
if (moment('2014-11-11').diff(moment('2014-10-11'),'years') != 0) throw ''; | |
// | |
// isAfter | |
// | |
if (moment('2014-11-11').isBefore(moment('2014-10-11'))) throw ''; | |
if (moment('2014-11-11').isBefore(moment('2014-10-24'))) throw ''; | |
if (moment('2014-11-12').isAfter(moment('2014-11-16'))) throw ''; | |
if (moment('2014-11-19').isAfter(moment('2014-12-11'))) throw ''; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment