日期格式化函数

function timestampToTime(val,to) {
    var val = new Date(val)
    var year = val.getFullYear();
    var month = (val.getMonth()+1 < 10 ? '0'+(val.getMonth()+1) : val.getMonth()+1);
    var date = (val.getDate() < 10 ? '0'+(val.getDate()) : val.getDate());
    var hour = (val.getHours() < 10 ? '0'+(val.getHours()) : val.getHours());
    var minute = (val.getMinutes() < 10 ? '0'+(val.getMinutes()) : val.getMinutes());
    var second = (val.getSeconds() < 10 ? '0'+(val.getSeconds()) : val.getSeconds());
    switch (to){
        case 'day':
            return year+'-'+month+'-'+date;
            break;
        case 'second':
            return year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second;
            break;
    }
}

 

日期格式化函数。不够补零。

posted @ 2018-12-29 11:06  Hello_nico  阅读(224)  评论(0编辑  收藏  举报