最近開發中需要和后端進日期和時間傳值,前后端約定為時間戳的格式,但是前端展示需要展示成年-月-日的格式。就需要進行日期和時間轉換格式。自己總結兩個方式就行轉換。
一,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 ''; } },
