javascript时间戳转换成yyyy-MM-DD格式

 

最近开发中需要和后端进日期和时间传值,前后端约定为时间戳的格式,但是前端展示需要展示成年-月-日的格式。就需要进行日期和时间转换格式。自己总结两个方式就行转换。

一,new Date(时间戳).format("yyyy-MM-dd");

二,是封装的函数转换

    

formatDayTime: function(val) {
                if(val) {
                    let date = new Date(val)
                    let Y = date.getFullYear();
                    let M = date.getMonth() + 1;
                    let D = date.getDate();

                    if(M < 10) {
                        M = '0' + M;
                    }
                    if(D < 10) {
                        D = '0' + D;
                    }

                    return Y + '-' + M + '-' + D ;
                } else {
                    return '';
                }
            },

 

posted @ 2017-10-21 14:29  萧诺  阅读(8845)  评论(0)    收藏  举报