- qunit-1.11.0.js
- qunit-1.11.0.css
- ISO8601.js
- testcases.js
- qunit.html
前兩項從QUnit官方網站下載。
ISO8601.js
Date.prototype.setISO8601 = function(string) {
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})"
+ "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?"
+ "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
var d = string.match(new RegExp(regexp));
var offset = 0;
var date = new Date(d[1], 0, 1);
if (d[3]) {
date.setMonth(d[3] - 1);
}
if (d[5]) {
date.setDate(d[5]);
}
if (d[7]) {
date.setHours(d[7]);
}
if (d[8]) {
date.setMinutes(d[8]);
}
if (d[10]) {
date.setSeconds(d[10]);
}
if (d[12]) {
date.setMilliseconds(Number("0." + d[12]) * 1000);
}
if (d[14]) {
offset = (Number(d[16]) * 60) + Number(d[17]);
offset *= ((d[15] == '-') ? 1 : -1);
}
offset -= date.getTimezoneOffset();
time = (Number(date) + (offset * 60 * 1000));
this.setTime(Number(time));
};
Date.prototype.getISO8601 = function() {
return this.getFullYear() + '-' + (this.getMonth() + 1).format(2) + '-'
+ this.getDate().format(2) + 'T' + this.getHours().format(2) + ':'
+ this.getMinutes().format(2) + ':' + this.getSeconds().format(2)
+ '.' + this.getMilliseconds().format(3) + this.getTimezone();
};
Date.prototype.getTimezone = function() {
var offset = -this.getTimezoneOffset();
if (offset == 0) {
return "Z";
}
var hour = parseInt(Math.abs(offset / 60)).format(2);
var minute = Math.abs(offset % 60).format(2)
var sign = offset > 0 ? "+" : "-";
return sign + hour + ":" + minute;
};
Number.prototype.format = function(length) {
var str = "" + this;
while (str.length < length) {
str = '0' + str;
}
return str;
};
var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})"
+ "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?"
+ "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
var d = string.match(new RegExp(regexp));
var offset = 0;
var date = new Date(d[1], 0, 1);
if (d[3]) {
date.setMonth(d[3] - 1);
}
if (d[5]) {
date.setDate(d[5]);
}
if (d[7]) {
date.setHours(d[7]);
}
if (d[8]) {
date.setMinutes(d[8]);
}
if (d[10]) {
date.setSeconds(d[10]);
}
if (d[12]) {
date.setMilliseconds(Number("0." + d[12]) * 1000);
}
if (d[14]) {
offset = (Number(d[16]) * 60) + Number(d[17]);
offset *= ((d[15] == '-') ? 1 : -1);
}
offset -= date.getTimezoneOffset();
time = (Number(date) + (offset * 60 * 1000));
this.setTime(Number(time));
};
Date.prototype.getISO8601 = function() {
return this.getFullYear() + '-' + (this.getMonth() + 1).format(2) + '-'
+ this.getDate().format(2) + 'T' + this.getHours().format(2) + ':'
+ this.getMinutes().format(2) + ':' + this.getSeconds().format(2)
+ '.' + this.getMilliseconds().format(3) + this.getTimezone();
};
Date.prototype.getTimezone = function() {
var offset = -this.getTimezoneOffset();
if (offset == 0) {
return "Z";
}
var hour = parseInt(Math.abs(offset / 60)).format(2);
var minute = Math.abs(offset % 60).format(2)
var sign = offset > 0 ? "+" : "-";
return sign + hour + ":" + minute;
};
Number.prototype.format = function(length) {
var str = "" + this;
while (str.length < length) {
str = '0' + str;
}
return str;
};
testcases.js
test( "iso8601 test1", function() {
var date = new Date();
date.setISO8601('2012-01-30T16:07:03.007Z');
equal(date.getISO8601(), '2012-01-31T00:07:03.007+08:00');
});
test( "iso8601 test2", function() {
var date = new Date();
date.setISO8601('2012-01-30T16:07:03.012+08:00');
equal(date.getISO8601(), '2012-01-30T16:07:03.012+08:00');
});
test( "iso8601 test3", function() {
var today = new Date();
var milliseconds = today.valueOf();
today.setISO8601(today.getISO8601());
equal(today.valueOf() == milliseconds, true);
});
var date = new Date();
date.setISO8601('2012-01-30T16:07:03.007Z');
equal(date.getISO8601(), '2012-01-31T00:07:03.007+08:00');
});
test( "iso8601 test2", function() {
var date = new Date();
date.setISO8601('2012-01-30T16:07:03.012+08:00');
equal(date.getISO8601(), '2012-01-30T16:07:03.012+08:00');
});
test( "iso8601 test3", function() {
var today = new Date();
var milliseconds = today.valueOf();
today.setISO8601(today.getISO8601());
equal(today.valueOf() == milliseconds, true);
});
qunit.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit Example</title>
<link rel="stylesheet" href="qunit-1.11.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="ISO8601.js"></script>
<script src="qunit-1.11.0.js"></script>
<script src="testcases.js"></script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit Example</title>
<link rel="stylesheet" href="qunit-1.11.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="ISO8601.js"></script>
<script src="qunit-1.11.0.js"></script>
<script src="testcases.js"></script>
</body>
</html>
Test Result