js格式化时间

转自:https://blog.csdn.net/u010964869/article/details/51095827

显示格式为:yyyy-mm-dd hh:mi:ss


function formattime(val){

if(val == "" || val == null || val == undefined){
return "";
}
var date = new Date(val);
var year = parseInt(date.getFullYear());
var month = (parseInt(date.getMonth())+1);
var day = parseInt(date.getDate());
day = day > 9 ? day:('0'+day);
var hours = parseInt(date.getHours());
hours = hours > 9 ? hours:('0'+hours);
var minutes = parseInt(date.getMinutes());
minutes = minutes > 9 ? minutes:('0'+minutes);
var seconds = parseInt(date.getSeconds());
seconds = seconds > 9 ? seconds:('0'+seconds);
var time = year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+seconds;

return time;
}
 

 注:parseInt可以不要

posted @ 2019-03-26 17:48  無玑小姐  阅读(193)  评论(0编辑  收藏  举报