From 5978924bb62dd1f6db0d81c46219391b6d5d052a Mon Sep 17 00:00:00 2001 From: Tor-Einar Skog <tor-einar.skog@nibio.no> Date: Mon, 26 Nov 2018 11:08:35 +0100 Subject: [PATCH] Using momentjs to parse date string, to ensure it works in Safari --- src/main/webapp/js/util.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/webapp/js/util.js b/src/main/webapp/js/util.js index 74c3f49d..025de9e6 100755 --- a/src/main/webapp/js/util.js +++ b/src/main/webapp/js/util.js @@ -175,14 +175,25 @@ function getLocalizedOptionsHTML(optionsList) { return translatedOptionsHTML; } -/** Ensure that we're able to handle both a unix timestamp and an ISO timestamp +/** + * Ensure that we're able to handle both a unix timestamp and an ISO timestamp + * Unfortunately, this method depends on momentjs, since date parsing directly in JavaScript is...hard * * @param {type} ambiguousValue * @returns {Number} */ function getUnixTimestampFromJSON(ambiguousValue) { - var possibleDateObject = new Date(ambiguousValue); + var possibleDateObject; + if(!isMomentJSAvailable()) + { + possibleDateObject = new Date(ambiguousValue); + console.info("Warning: Parsing date without MomentJS. Can't guarantee correct result."); + } + else + { + possibleDateObject = moment(ambiguousValue).toDate(); + } if(possibleDateObject.getTime() === NaN && typeof parseInt(ambiguousValue) === "number") { return parseInt(ambiguousValue); @@ -191,4 +202,22 @@ function getUnixTimestampFromJSON(ambiguousValue) { return possibleDateObject.getTime(); } +} + +/** + * Does what it says + * + * @return {Boolean} + */ +function isMomentJSAvailable() +{ + try { + moment(); + return true; + } + catch (err) + { + return false; + } + } \ No newline at end of file -- GitLab