当前位置 : 主页 > 网络编程 > JavaScript >

通过当前时间或者时间戳对时间格式化

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt var times = function(d, sec) { var time; var date = new Date(d); var y = date.getFullYear(); var M = date.getMonth() + 1; M = M 10 ? '0' + M : M; var d = date.getDate(); d = d 10 ? '0' + d : d; var h = date.getHours(); h = h 1
gistfile1.txt
var times = function(d, sec) {
		  var time;
		  var date = new Date(d);
		  var y = date.getFullYear();
		  var M = date.getMonth() + 1;
		  M = M < 10 ? '0' + M : M;
		  var d = date.getDate();
		  d = d < 10 ? '0' + d : d;
		  var h = date.getHours();
		  h = h < 10 ? '0' + h : h;
		  var m = date.getMinutes();
		  m = m < 10 ? '0' + m : m;
		  var s = date.getSeconds();
		  s = s < 10 ? '0' + s : s;
		  if (sec) {
		    time = y + '-' + M + '-' + d + ' ' + h + ':' + m + ':' + s;
		  } else {
		    time = y + '-' + M + '-' + d + ' ' + h + ':' + m;
		  }
		
		  return time;
};
网友评论