function Moment(date, isUTC) {
this._d = date;
this._isUTC = !!isUTC;
+ this._a = date.__origin || 0;
+ date.__origin = null;
}
function absRound(number) {
// the array should mirror the parameters below
// note: all values past the year are optional and will default to the lowest possible value.
// [year, month, day , hour, minute, second, millisecond]
- function dateFromArray(input) {
- return new Date(input[0], input[1] || 0, input[2] || 1, input[3] || 0, input[4] || 0, input[5] || 0, input[6] || 0);
+ function dateFromArray(input, asUTC) {
+ input[2] = input[2] || 1;
+ var date = asUTC ? new Date(Date.UTC.apply({}, input)) :
+ new Date(input[0], input[1] || 0, input[2], input[3] || 0, input[4] || 0, input[5] || 0, input[6] || 0);
+ date.__origin = input;
+ return date;
}
// format date using native date object
return this._d;
},
+ toArray : function () {
+ var m = this;
+ return [
+ m.year(),
+ m.month(),
+ m.date(),
+ m.hours(),
+ m.minutes(),
+ m.seconds(),
+ m.milliseconds()
+ ];
+ },
+
+ isValid : function () {
+ var i = 0,
+ toArray = this.toArray();
+ for (; this._a && i < 7; i++) {
+ if ((this._a[i] || 0) !== toArray[i]) {
+ return false;
+ }
+ }
+ return !isNaN(this._d.getTime());
+ },
+
utc : function () {
this._isUTC = true;
return this;
--- /dev/null
+var moment = require("../../moment");
+
+exports.is_valid = {
+ "array bad month" : function(test) {
+ test.expect(2);
+
+ test.equal(moment([2010, -1]).isValid(), false, 'month -1');
+ test.equal(moment([2100, 12]).isValid(), false, 'month 12');
+
+ test.done();
+ },
+
+ "array good month" : function(test) {
+ test.expect(12);
+
+ for (var i = 0; i < 12; i++) {
+ test.equal(moment([2010, i]).isValid(), true, 'month ' + i);
+ }
+
+ test.done();
+ },
+
+ "array bad date" : function(test) {
+ test.expect(2);
+
+ test.equal(moment([2010, 0, 0]).isValid(), false, 'date 0');
+ test.equal(moment([2100, 0, 32]).isValid(), false, 'date 32');
+
+ test.done();
+ },
+
+ "array bad date leap year" : function(test) {
+ test.expect(4);
+
+ test.equal(moment([2010, 1, 29]).isValid(), false, '2010 feb 29');
+ test.equal(moment([2100, 1, 29]).isValid(), false, '2100 feb 29');
+ test.equal(moment([2008, 1, 30]).isValid(), false, '2008 feb 30');
+ test.equal(moment([2000, 1, 30]).isValid(), false, '2000 feb 30');
+ test.done();
+ },
+
+ "string nonsensical" : function(test) {
+ test.expect(1);
+
+ test.equal(moment('fail').isValid(), false, 'string "fail"');
+ test.done();
+ }
+};