Skip to content

Instantly share code, notes, and snippets.

@nickcoutsos
Created February 19, 2012 03:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickcoutsos/1861793 to your computer and use it in GitHub Desktop.
Save nickcoutsos/1861793 to your computer and use it in GitHub Desktop.
Timestamp parser bookmarklet. Highlight a timestamp (or any number) and use the bookmarklet to display it parsed as a date and time, in your locale using your browser's timezone and UTC.
javascript:(function(){findSelectionDocument=function(w){if(w.document.getSelection().toString().length>0){return w.document;}for(var i=0;i<w.frames.length;i++){var doc=findSelectionDocument(w.frames[i]);if(doc){return doc;}}return null;};doc=findSelectionDocument(window)||document;timestamp=doc.getSelection().toString().trim();c=doc.getElementById("_canvas");if(c)return;c=doc.createElement("div");c.id="_canvas";c.setAttribute("style","position:fixed;z-index:100;background-color:rgba(25,25,25,.90);top:0px;left:0px;height:100%;width:100%;text-align:center;font-family:monospace, sans-serif;font-size:12px;");c.onclick=function(){this.parentNode.removeChild(this);};w=doc.createElement("div");w.setAttribute("style","background-color:white;padding:20px;margin:100px auto 0;display:inline-block;border-radius:10px;text-align:left;box-shadow:5px 8px 24px -10px black;");w.onclick=function(){event.stopPropagation();};t=doc.createElement("table");t.setAttribute("style","border-collapse:separate;border-spacing:20px 4px;");if(timestamp.length>0){timestamp=parseInt(timestamp,10);d=new Date(timestamp*1000);u=new Date((timestamp+d.getTimezoneOffset()*60)*1000);t.innerHTML="<tr><th>Timestamp:</th><td>"+timestamp+"</td></tr>"+"<tr><th>Local:</th><td>"+d.toLocaleString()+"</td></tr>"+"<tr><th>UTC:</th><td>"+u.toLocaleString().slice(0,-15)+"</td></tr>";}else{t.innerHTML="<tr><td>Ain't no timestamp!</td></tr>";}if(doc.getElementsByTagName("body").length>0){doc.body.appendChild(c);c.appendChild(w);w.appendChild(t);}})();
findSelectionDocument = function(w) {
if (w.document.getSelection().toString().length > 0) {
return w.document;
}
for (var i = 0; i < w.frames.length; i++) {
var doc = findSelectionDocument(w.frames[i]);
if (doc) {
return doc;
}
}
return null;
};
doc = findSelectionDocument(window) || document;
timestamp = doc.getSelection().toString().trim();
c = doc.getElementById("_canvas");
if (c) return;
c = doc.createElement("div");
c.id = "_canvas";
c.setAttribute("style", "position:fixed;z-index:100;background-color:rgba(25,25,25,.90);top:0px;left:0px;height:100%;width:100%;text-align:center;font-family:monospace, sans-serif;font-size:12px;");
c.onclick = function() {
this.parentNode.removeChild(this);
};
w = doc.createElement("div");
w.setAttribute("style", "background-color:white;padding:20px;margin:100px auto 0;display:inline-block;border-radius:10px;text-align:left;box-shadow:5px 8px 24px -10px black;");
w.onclick = function() {
event.stopPropagation();
};
t = doc.createElement("table");
t.setAttribute("style", "border-collapse:separate;border-spacing:20px 4px;");
if (timestamp.length > 0) {
timestamp = parseInt(timestamp, 10);
d = new Date(timestamp * 1000);
u = new Date((timestamp + d.getTimezoneOffset() * 60) * 1000);
t.innerHTML = "<tr><th>Timestamp:</th><td>" + timestamp + "</td></tr>" + "<tr><th>Local:</th><td>" + d.toLocaleString() + "</td></tr>" + "<tr><th>UTC:</th><td>" + u.toLocaleString().slice(0, -15) + "</td></tr>";
} else {
t.innerHTML = "<tr><td>Ain't no timestamp!</td></tr>";
}
if (doc.getElementsByTagName("body").length > 0) {
doc.body.appendChild(c);
c.appendChild(w);
w.appendChild(t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment