Skip to content
Snippets Groups Projects
Commit 5978924b authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Using momentjs to parse date string, to ensure it works in Safari

parent e1c4efe9
No related branches found
No related tags found
No related merge requests found
...@@ -175,14 +175,25 @@ function getLocalizedOptionsHTML(optionsList) { ...@@ -175,14 +175,25 @@ function getLocalizedOptionsHTML(optionsList) {
return translatedOptionsHTML; 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 * @param {type} ambiguousValue
* @returns {Number} * @returns {Number}
*/ */
function getUnixTimestampFromJSON(ambiguousValue) 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") if(possibleDateObject.getTime() === NaN && typeof parseInt(ambiguousValue) === "number")
{ {
return parseInt(ambiguousValue); return parseInt(ambiguousValue);
...@@ -191,4 +202,22 @@ function getUnixTimestampFromJSON(ambiguousValue) ...@@ -191,4 +202,22 @@ function getUnixTimestampFromJSON(ambiguousValue)
{ {
return possibleDateObject.getTime(); 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment