js中Date对象,日期格式化

// js中Date对象
/**orm

  • new Date() new Date(时间戳) 获取标准时间
  • /
    let date = new Date();// 当前时间 Tue Dec 03 2019 11:17:30 GMT+0800 (中国标准时间)
    let date = new Date(1575342963325);// 已知时间戳转标准时间 Tue Dec 03 2019 11:16:03 GMT+0800 (中国标准时间)
    /
    时间转时间戳 */
    let timeStamp = date.getTime();// getTime() 返回 1970 年 1 月 1 日至今的毫秒数
    // console.log(timeStamp);

/* 从Date对象(标准时间格式)返回对应数据 */
let week = date.getDay();// getDay() 返回一周中的某一天 (0 ~ 6)对象

/* 月份须要+1 */
let year=date.getFullYear();// getFullYear() 返回年
let month=date.getMonth()+1;// getMonth() 返回月份 (0 ~ 11)
let day = date.getDate();// getDate() 返回日 (1 ~ 31)
let hours=date.getHours(); // getHours() 返回小时 (0 ~ 23)
let minutes=date.getMinutes();// getMinutes() 返回分(0 ~ 59)
let seconds=date.getSeconds();// getSeconds() 返回秒(0 ~ 59)get

举个栗子
/将后台时间戳数据转为标准时间/
function formatTime(date) {
/* 从Date对象(标准时间格式)返回对应数据 */
var date = new Date(date);
let year = date.getFullYear();
let month = date.getMonth();
let day = date.getDate();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
return year + '年' + month + '月' + day+'日' ;
}
console.log(formatTime(1575344332354));io

相关文章
相关标签/搜索