时间戳格式转换

时间戳转换格式为年月日星期几

/**
 *[formatDate 转换事件格式并返回html片段]
 * author: LiuWei
 * time: 2017/9/19
 * @param  {[type]} timestamp [时间戳]
 * @return {[type]}           [时间html片段]
 */
function formatDate(timestamp) {
    var dateObj = new Date(timestamp);
    var theYear = dateObj.getFullYear();
    var theMonth = dateObj.getMonth();
    var theDay = dateObj.getDate();
    var theWeekDay = dateObj.getDay();
    var theHour = dateObj.getHours();
    var theMinute = dateObj.getMinutes();
    switch (theWeekDay) {
        case 0:
            theWeekDay = '周日';
            break;
        case 1:
            theWeekDay = '周一';
            break;
        case 2:
            theWeekDay = '周二';
            break;
        case 3:
            theWeekDay = '周三';
            break;
        case 4:
            theWeekDay = '周四';
            break;
        case 5:
            theWeekDay = '周五';
            break;
        case 6:
            theWeekDay = '周六';
            break;
    }
    console.log(theWeekDay);
    var dateHtml = '<span class="day">' + theYear + '.' +
        (theMonth + 1 < 10 ? '0' + (theMonth + 1) : (theMonth + 1)) + '.' +
        (theDay < 10 ? '0' + theDay : theDay) + '</span>' +
        '<span class="week">' + (theWeekDay < 10 ? '0' + theWeekDay : theWeekDay) + '</span>' +
        '<span class="time">' + (theHour < 10 ? '0' + theHour : theHour) + ':' + (theMinute < 10 ? '0' + theMinute : theMinute) + '</span>';
    console.log(dateHtml);
    return dateHtml;
}
原文地址:https://www.cnblogs.com/lw5116/p/7791920.html