记录项目中需要处理的日期格式 默认vue2
初级版
将后端传来的数组
['2024/01/29 08:55:18', '2024/01/29 09:55:18', '2024/01/29 10:11:18']
转为
['2024-01-29 08:55', '2024-01-29 09:55', '2024-01-29 10:11']
方法
convertDateTimeFormat(arr) {var tempArr = arr.map(function (dateTime) {var date = new Date(dateTime)var formattedDateTime =date.getFullYear() +'-' +('0' + (date.getMonth() + 1)).slice(-2) +'-' +('0' + date.getDate()).slice(-2) +' ' +('0' + date.getHours()).slice(-2) +':' +('0' + date.getMinutes()).slice(-2)return formattedDateTime})return tempArr},
如果只传入
'2024/01/29 08:55:18'
转
'2024-01-29 08:55'
convertDateTimeFormat(dateTime) {var date = new Date(dateTime)var formattedDateTime =date.getFullYear() +'-' +('0' + (date.getMonth() + 1)).slice(-2) +'-' +('0' + date.getDate()).slice(-2) +' ' +('0' + date.getHours()).slice(-2) +':' +('0' + date.getMinutes()).slice(-2)return formattedDateTime},
传入时间戳数字类型
var curveTimestamp = 1706227228000
转
'2024-01-29 08:55'
方法
convertDateTimeFormat(timestamp) {var date = new Date(timestamp)var formattedDateTime =date.getFullYear() +'-' +('0' + (date.getMonth() + 1)).slice(-2) +'-' +('0' + date.getDate()).slice(-2) +' ' +('0' + date.getHours()).slice(-2) +':' +('0' + date.getMinutes()).slice(-2)return formattedDateTime},