我获取一个出生日期,如:19940101 1994-01-01 1994.01.01 1994/01/01 1994-1-1 1994.1.1 1994/1/1,就可能是其中一种日期,我该怎么样才能将其转换为yyyy-mm-dd的格式?
已解决
悬赏分:30
- 解决时间 2021-11-29 07:10
点赞 0反对 0举报 0
收藏 0
分享 0
回答3
最佳
-
function formatTime(str) { return str.replace(/\D/g,'') .replace(/(\d{4})(\d{1,2})(\d{1,2})/, (_,$1,$2,$3) => $1 + '-' + $2.padStart(2,'0') + '-' + $3.padStart(2,'0')) }
支持 0 反对 0 举报2021-11-28 08:14
-
除了第一个,其他都应该适用
function getFormatDate(date, dateType) { let dateObj = new Date(date); let month = dateObj.getMonth() + 1; let strDate = dateObj.getDate(); let hours = dateObj.getHours(); let minutes = dateObj.getMinutes(); let seconds = dateObj.getSeconds(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } if (hours >= 0 && hours <= 9) { hours = "0" + hours } if (minutes >= 0 && minutes <= 9) { minutes = "0" + minutes } if (seconds >= 0 && seconds <= 9) { seconds = "0" + seconds } let dateText = dateObj.getFullYear() + '年' + (dateObj.getMonth() + 1) + '月' + dateObj.getDate() + '日'; if (dateType == "yyyy-mm-dd") { dateText = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate(); } if (dateType == "yyyy.mm.dd") { dateText = dateObj.getFullYear() + '.' + (dateObj.getMonth() + 1) + '.' + dateObj.getDate(); } if (dateType == "yyyy-mm-dd MM:mm:ss") { dateText = dateObj.getFullYear() + '-' + month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds; } if (dateType == "mm-dd MM:mm:ss") { dateText = month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds; } if (dateType == "yyyy年mm月dd日 MM:mm:ss") { dateText = dateObj.getFullYear() + '年' + month + '月' + strDate + '日' + ' ' + hours + ":" + minutes + ":" + seconds; } return dateText; }
;
支持 0 反对 0 举报2021-11-28 08:54