Skip to content

Instantly share code, notes, and snippets.

@daneko
Created November 15, 2013 06:00
Show Gist options
  • Save daneko/7479824 to your computer and use it in GitHub Desktop.
Save daneko/7479824 to your computer and use it in GitHub Desktop.
mongoで時間出力の見た目を整形したくてjavascript書きました javascript力がなさすぎて悲しくなる javascript date format
var dformat = function(d){
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var date = new Date(utc);
var tos = function(t){
if(t < 10) return "0" + t;
return "" + t;
}
var y = tos(date.getFullYear());
var M = tos(date.getMonth() + 1);
var d = tos(date.getDate());
var h = tos(date.getHours());
var m = tos(date.getMinutes());
var s = tos(date.getSeconds());
var ymd = [ y, M, d].reduce(function(tmp,val){return tmp + "-" + val});
var hms = [ h, m, s].reduce(function(tmp,val){return tmp + ":" + val});
return ymd + " " + hms;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment