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

相關文章
相關標籤/搜索