From 9096a5ff0008dda08314a0e814e32467674cce23 Mon Sep 17 00:00:00 2001 From: ioncreature Date: Wed, 24 Dec 2014 22:52:53 +0300 Subject: [PATCH] Added Date.isValid static method. Added isValid. toStartOfDay, toEndOfDay, toStartOfUTCDay, toEndOfUTCDay instance methods --- lib/date-utils.js | 83 +++++++++++++++++++++++++++++++++++++- test/date-new-test.js | 26 ++++++++++++ test/date-validate-test.js | 19 +++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) diff --git a/lib/date-utils.js b/lib/date-utils.js index 6536733..ea6e976 100644 --- a/lib/date-utils.js +++ b/lib/date-utils.js @@ -475,6 +475,19 @@ THE SOFTWARE. return Date.UTCtoday().add({days: -1}); }; + /** + Returns whether the date is valid + @static + @param date {Number|String|Date} timestamp or date string or date + @returns {Boolean} + @function isValid + @memberof Date + */ + Date.isValid = function (date) { + var d = date instanceof Date ? date : new Date(date); + return !isNaN(d.valueOf()); + }; + /** Returns whether the day is valid @static @@ -779,7 +792,7 @@ THE SOFTWARE. }); /** - Sets the time to 00:00:00.0000 and returns a new Date object + Sets the time to 00:00:00.000 and returns a Date object @returns {Date} @function clearTime @instance @@ -795,7 +808,45 @@ THE SOFTWARE. }); /** - Sets the time to 00:00:00.0000 and returns a new Date object with set to UTC + @returns {Boolean} + @function isValid + @instance + @memberof Date + */ + polyfill('isValid', function () { + return !isNaN(this.valueOf()); + }); + + /** + Sets the time to 00:00:00.000 and returns a Date object + @alias clearTime + @returns {Date} + @function toStartOfDay + @instance + @memberof Date + */ + polyfill('toStartOfDay', function () { + return this.clearTime() + }); + + /** + Sets the time to 23:59:59.999 and returns a Date object + @returns {Date} + @function toEndOfDay + @instance + @memberof Date + */ + polyfill('toEndOfDay', function () { + this.setHours(23); + this.setMinutes(59); + this.setSeconds(59); + this.setMilliseconds(999); + + return this; + }); + + /** + Sets the time to 00:00:00.000 and returns a new Date object with set to UTC @returns {Date} @function clearUTCTime @instance @@ -810,6 +861,34 @@ THE SOFTWARE. return this; }); + /** + Sets the time to 00:00:00.000 and returns a Date object with set to UTC + @alias clearUTCTime + @returns {Date} + @function toStartOfUTCDay + @instance + @memberof Date + */ + polyfill('toStartOfUTCDay', function () { + return this.clearUTCTime(); + }); + + /** + Sets the time to UTC 23:59:59.999 and returns a Date object + @returns {Date} + @function toEndOfUTCDay + @instance + @memberof Date + */ + polyfill('toEndOfUTCDay', function () { + this.setUTCHours(23); + this.setUTCMinutes(59); + this.setUTCSeconds(59); + this.setUTCMilliseconds(999); + + return this; + }); + /** Adds `milliseconds`, `seconds`, `minutes`, `hours`, `days`, `weeks`, `months`, and `years` and returns a new Date. Usage: `data.add({ "seconds": 10, "days": 1 })` diff --git a/test/date-new-test.js b/test/date-new-test.js index ac00c45..368dd5b 100644 --- a/test/date-new-test.js +++ b/test/date-new-test.js @@ -38,6 +38,32 @@ vows.describe('Date New').addBatch({ } }, + 'toEndOfDay() works': { + topic: function() { return new Date().toEndOfDay(); }, + 'returns the correct value': function (date) { + var compare = new Date(); + compare.setHours(23); + compare.setMinutes(59); + compare.setSeconds(59); + compare.setMilliseconds(999); + + assert.equal(date.valueOf(), compare.valueOf()); + } + }, + + 'toEndOfUTCDay() works': { + topic: function() { return new Date().toEndOfUTCDay(); }, + 'returns the correct value': function (date) { + var compare = new Date(); + compare.setUTCHours(23); + compare.setUTCMinutes(59); + compare.setUTCSeconds(59); + compare.setUTCMilliseconds(999); + + assert.equal(date.valueOf(), compare.valueOf()); + } + }, + 'today() works': { topic: function() { return Date.today(); diff --git a/test/date-validate-test.js b/test/date-validate-test.js index 6cb3be8..9f8d755 100644 --- a/test/date-validate-test.js +++ b/test/date-validate-test.js @@ -4,6 +4,25 @@ var assert = require('assert'); require('../lib/date-utils.js'); vows.describe('Date Validate').addBatch({ + 'can validate date object': { + 'return false for invalid date': function () { + var invalidDate = new Date('this is invalid date string'); + assert.equal(Date.isValid(invalidDate), false); + assert.equal(invalidDate.isValid(), false); + }, + 'return false for invalid date again': function () { + var invalidDate = new Date(); + invalidDate.setDate( 'this is invalid day number' ); + assert.equal(Date.isValid(invalidDate), false); + assert.equal(invalidDate.isValid(), false); + }, + 'return true for valid date': function () { + var correctDate = new Date; + assert.equal( Date.isValid(correctDate), true ); + assert.equal( correctDate.isValid(), true ); + } + }, + 'can deal with hours': { topic: function () { return Date; }, 'false for less than 0': function (topic) {