Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Date.isValid static method. Added isValid, toStartOfDay, toEndOfDay, toStartOfUTCDay, toEndOfUTCDay instance methods #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions lib/date-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 })`
Expand Down
26 changes: 26 additions & 0 deletions test/date-new-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions test/date-validate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down