时间戳转化为日期

时间戳转化为日期

/**
 * 时间戳转化为日期
 * @param timestamp 秒,毫秒均可
 * @returns {string} 返回 年-月-日 时:分:秒
 * @constructor
 */
TimestampToTime = function (timestamp) {
    if (typeof timestamp == 'number'){
        timestamp = timestamp + '';
    }
    var date;
    //时间戳为10位需*1000,时间戳为13位的话不需乘1000
    if (timestamp.length > 10){
        date = new Date(parseInt(timestamp));
    }else {
        date = new Date(parseInt(timestamp) * 1000);
    }
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes()) + ':';
    var s = (date.getSeconds() < 10 ? '0'+date.getSeconds() : date.getSeconds());
    return Y+M+D+h+m+s;
};

猜你喜欢

转载自my.oschina.net/u/3756690/blog/2872328