Add Zotero.Date.isISODate() and Zotero.Date.isoToSQL()

This commit is contained in:
Dan Stillman 2015-04-29 17:26:05 -04:00
parent e8d4b3e840
commit 322339876e
2 changed files with 26 additions and 2 deletions

View file

@ -180,6 +180,15 @@ Zotero.Date = new function(){
}
var _re8601 = /^([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})))?)?)?)?$/;
/**
* @return {Boolean} - True if string is an ISO 8601 date, false if not
*/
this.isISODate = function (str) {
return _re8601.test(str);
}
/**
* Convert an ISO 8601formatted UTC date/time to a JS Date
*
@ -189,8 +198,7 @@ Zotero.Date = new function(){
* @return {Date} JS Date
*/
this.isoToDate = function (isoDate) {
var re8601 = /([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 = isoDate.match(re8601);
var d = isoDate.match(_re8601);
var offset = 0;
var date = new Date(d[1], 0, 1);
@ -212,6 +220,11 @@ Zotero.Date = new function(){
}
this.isoToSQL = function (isoDate) {
return this.dateToSQL(this.isoToDate(isoDate), true);
}
/*
* converts a string to an object containing:
* day: integer form of the day

11
test/tests/dateTest.js Normal file
View file

@ -0,0 +1,11 @@
describe("Zotero.Date", function() {
describe("#isISODate()", function () {
it("should determine whether a date is an ISO 8601 date", function () {
assert.ok(Zotero.Date.isISODate("2015"));
assert.ok(Zotero.Date.isISODate("2015-04"));
assert.ok(Zotero.Date.isISODate("2015-04-29"));
assert.ok(Zotero.Date.isISODate("2015-04-29T17:28Z"));
assert.isFalse(Zotero.Date.isISODate("2015-04-29 17:28"));
})
})
})