Skip to content

Instantly share code, notes, and snippets.

@phplaw
Last active August 13, 2020 07:14
Show Gist options
  • Select an option

  • Save phplaw/1c148b91dfe447a30ec642054da7d296 to your computer and use it in GitHub Desktop.

Select an option

Save phplaw/1c148b91dfe447a30ec642054da7d296 to your computer and use it in GitHub Desktop.
// pretty log object
var x = [{a:1}, {a:2}, {a:3}];
var log = console.log;
log(x.unshift({a:4}, {a:5}));
log(JSON.stringify(x, null, 2));
// to mysql datetime format
function twoDigits(d) {
return d.toString().padStart(2,0);
/*
if(0 <= d && d < 10) return "0" + d.toString();
if(-10 < d && d < 0) return "-0" + (-1*d).toString();
return d.toString();*/
}
Date.prototype.toMysqlFormat = function() {
return this.getFullYear() + "-" + twoDigits(1 + this.getMonth()) + "-" + twoDigits(this.getDate()) + " " + twoDigits(this.getHours()) + ":" + twoDigits(this.getMinutes()) + ":" + twoDigits(this.getSeconds());
};
console.log(new Date().toMysqlFormat());
// new Date().toMysqlFormat() or
// new Date(2014,12,14).toMysqlFormat() or whatever.
// end to mysql datetime format
// iput date to mysql date
var x = "22/06/2020".split('/').reverse().join('-');
console.log(x);
// 30 minutes from now
let thirtyMinutes = 30 * 60 * 1000; // convert 30 minutes to milliseconds
let date1 = new Date();
let date2 = new Date(date1.getTime() + thirtyMinutes);
console.log(date1);
console.log(date2);
function addMinute(minutes) {
var dt = new Date();
dt.setMinutes( dt.getMinutes() + minutes );
return dt;
}
@phplaw
Copy link
Author

phplaw commented Aug 13, 2020

polyfill for Object.values

var obj = { foo: 'bar', baz: 42 };
var values = Object.keys(obj).map(function(e) {
  return obj[e]
})

console.log(values)

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