Skip to content

Instantly share code, notes, and snippets.

@shunjikonishi
Last active December 17, 2015 14:39
Show Gist options
  • Save shunjikonishi/5626130 to your computer and use it in GitHub Desktop.
Save shunjikonishi/5626130 to your computer and use it in GitHub Desktop.
yyyy-MM-dd HH:mm:ss形式のstrToDateとdateToStr
function strToDate(str) {
var y = parseInt(str.substring(0, 4)),
m = parseInt(str.substring(5, 7)),
d = parseInt(str.substring(8, 10)),
h = parseInt(str.substring(11, 13)),
mi = parseInt(str.substring(14, 16)),
s = parseInt(str.substring(17, 19)),
ms = 0;
return new Date(y, m - 1, d, h, mi, s, ms);
}
function dateToStr(d) {
function pad(n) {
return n < 10 ? "0" + n : "" + n;
}
return d.getFullYear() + "-" +
pad(d.getMonth() + 1) + "-" +
pad(d.getDate()) + " " +
pad(d.getHours()) + ":" +
pad(d.getMinutes()) + ":" +
pad(d.getSeconds());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment