JS时间戳转换 时间戳转时间 js转换时间戳为时间类型显示
本方法已经抽离出年月日时分秒 更多的时间格式搭配大家可以随意添加!!!
convertToEnglishMonthAbbreviation方法把月份转化成英文显示 大家可以看下 并非使用通过自定义枚举的方式实现。
formatTime方法通过传入24小时制时间 格式化为AM、PM
2024
2024-04
2024-04-14
2024-04-14 06:06
14 Apr 2024
2024/04/14
04-14 06:06
04/14 06:06
2024-04-14 06:06
2024/04/14 06:06
2024-04-14 06:06:06
2024/04/14 06:06:06
06:06:06
06:06
06:06 AM (AM/PM)
function formatTimeStamp(timeStamp, type) {let res = '';let date = new Date(timeStamp);let year = date.getFullYear();let month = (date.getMonth() + 1).toString().padStart(2, '0');let day = date.getDate().toString().padStart(2, '0');let hour = date.getHours().toString().padStart(2, '0');let minus = date.getMinutes().toString().padStart(2, '0');let second = date.getSeconds().toString().padStart(2, '0');switch (type) {case 'YYYY':res = `${year}`;break;case 'YYYY-MM':res = `${year}-${month}`;break;case 'YYYY-MM-dd':res = `${year}-${month}-${day}`;break;case 'dd EN YYYY':res = `${day} ${convertToEnglishMonthAbbreviation(month)} ${year}`;break;case 'YYYY/MM/dd':res = `${year}/${month}/${day}`;break;case 'MM-dd HH:mm':res = `${month}-${day} ${hour}:${minus}`;break;case 'MM/dd HH:mm':res = `${month}/${day} ${hour}:${minus}`;break;case 'YYYY-MM-dd HH:mm':res = `${year}-${month}-${day} ${hour}:${minus}`;break;case 'YYYY/MM/dd HH:mm':res = `${year}/${month}/${day} ${hour}:${minus}`;break;case 'YYYY-MM-dd HH:mm:ss':res = `${year}-${month}-${day} ${hour}:${minus}:${second}`;break;case 'YYYY/MM/dd HH:mm:ss':res = `${year}/${month}/${day} ${hour}:${minus}:${second}`;break;case 'HH:mm:ss':res = `${hour}:${minus}:${second}`;break;case 'HH:mm':res = `${hour}:${minus}`;break;case 'HH:mm M':let M=`${hour}:${minus}`;res = formatTime(M)break;}return res;
}function formatTime(time) {let hour = parseInt(time.split(':')[0]);let minute = parseInt(time.split(':')[1]);let period = hour >= 12 ? 'PM' : 'AM';hour = hour % 12;hour = hour ? hour : 12;let formattedTime = ('0' + hour).slice(-2) + ':' + ('0' + minute).slice(-2) + ' ' + period;return formattedTime;
}
function convertToEnglishMonthAbbreviation(month) {let date = new Date();date.setMonth(month - 1);let options = { month: 'short' };let englishMonthAbbreviation = date.toLocaleString('en-US', options); return englishMonthAbbreviation;
}formatTimeStamp(1325347200000,'YYYY')
formatTimeStamp(1325347200000,'YYYY-MM')
formatTimeStamp(1325347200000,'YYYY-MM-dd')
formatTimeStamp(1325347200000,'dd EN YYYY')
formatTimeStamp(1325347200000,'YYYY/MM/dd')
formatTimeStamp(1325347200000,'MM-dd HH:mm')
formatTimeStamp(1325347200000,'MM/dd HH:mm')
formatTimeStamp(1325347200000,'YYYY-MM-dd HH:mm')
formatTimeStamp(1325347200000,'YYYY/MM/dd HH:mm')
formatTimeStamp(1325347200000,'YYYY-MM-dd HH:mm:ss')
formatTimeStamp(1325347200000,'YYYY/MM/dd HH:mm:ss')
formatTimeStamp(1325347200000,'HH:mm:ss')
formatTimeStamp(1325347200000,'HH:mm')
formatTimeStamp(1325347200000,'HH:mm M')// 2024
// 2024-04
// 2024-04-14
// 2024-04-14 06:06
// 14 Apr 2024
// 2024/04/14
// 04-14 06:06
// 04/14 06:06
// 2024-04-14 06:06
// 2024/04/14 06:06
// 2024-04-14 06:06:06
// 2024/04/14 06:06:06
// 06:06:06
// 06:06
// 06:06 AM