Skip to content

Instantly share code, notes, and snippets.

@mark05e
Last active April 1, 2020 11:49
Show Gist options
  • Save mark05e/23419c68a38f1405a3675ff521911bc2 to your computer and use it in GitHub Desktop.
Save mark05e/23419c68a38f1405a3675ff521911bc2 to your computer and use it in GitHub Desktop.
My Javscript Snippets

getDate

function getDate(timestamp)
{
var date = new Date(timestamp);
var year = date.getFullYear();
var month = date.getMonth() + 1; // getMonth() is zero-indexed, so we'll increment to get the correct month number
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

month = (month < 10) ? '0' + month : month;
day = (day < 10) ? '0' + day : day;
hours = (hours < 10) ? '0' + hours : hours;
minutes = (minutes < 10) ? '0' + minutes : minutes;
seconds = (seconds < 10) ? '0' + seconds: seconds;

return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
}

ref: https://blog.serverdensity.com/automatic-timezone-conversion-in-javascript/

example: console.log ( getDate(1382086394000) )

traverseJson

function traverseJson(o) {
    for (var i in o) {
        if (!!o[i] && typeof(o[i])=="object") {
            console.log(i + " " + o[i]);
            traverseJson(o[i]);
        } else {
            console.log(i + " " + o[i]);
        }
    }
}

ref: https://stackoverflow.com/a/8986883/2854578

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment