function add0(m){return m<10?'0'+m:m } //时间戳转化成时间格式 function timeFormat(timestamp){ //timestamp是整数,否则要parseInt转换,不会出现少个0的情况 var time = new Date(timestamp); var year = time.getFullYear(); var month = time.getMonth()+1; var date = time.getDate(); var hours = time.getHours(); var minutes = time.getMinutes(); var seconds = time.getSeconds(); return year+'-'+add0(month)+'-'+add0(date)+' '+add0(hours)+':'+add0(minutes)+':'+add0(seconds); }
小程序里面写法
add0(m){ return m < 10 ? '0' + m : m }, //时间戳转化成时间格式 timeFormat(timestamp) { console.log(timestamp); //timestamp是整数,否则要parseInt转换,不会出现少个0的情况 var time = new Date(timestamp); var year = time.getFullYear(); var month = time.getMonth() + 1; var date = time.getDate(); var hours = time.getHours(); var minutes = time.getMinutes(); var seconds = time.getSeconds(); // return year + '-' + add0(month) + '-' + add0(date) + ' ' + add0(hours) + ':' + add0(minutes) + ':' + add0(seconds); return year + '-' + this.add0(month) + '-' + this.add0(date); },