JavaScript内置对象Date----格式化时间

格式化时间日期:

function getDate(dt) {
//获取年份
var year = dt.getFullYear();
//获取月份
var month = dt.getMonth();
//获取日
var day = dt.getDate();
//获取小时
var hour = dt.getHours();
//获取分钟
var minute = dt.getMinutes();
//获取秒
var second = dt.getSeconds();

month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;

return year + "年" + month + "月" + day +"日" + hour + ":" + minute + ":" + second;
}
console.log(getDate(new Date()));
posted @ 2018-07-15 18:48  {颜逸}  阅读(797)  评论(0编辑  收藏  举报