Add Zotero.Date.isISODate() and Zotero.Date.isoToSQL()
This commit is contained in:
parent
e8d4b3e840
commit
322339876e
2 changed files with 26 additions and 2 deletions
|
@ -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 8601–formatted 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
11
test/tests/dateTest.js
Normal 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"));
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Add table
Reference in a new issue