diff --git a/src/utils/class/date.js b/src/utils/class/date.js index 529fb78..72b7ef7 100644 --- a/src/utils/class/date.js +++ b/src/utils/class/date.js @@ -215,6 +215,29 @@ function parseTime(time, isOffset = false, cformat = null) { return result; } +/** + * 接口时间转换成12进制 + * @param {string|number} dateTimeStr 接口返回的时间和日期 + */ +function convertTo12HourFormat(dateTimeStr) { + // 解析输入的日期时间字符串 + const date = new Date(dateTimeStr.replace(' ', 'T')); // 使用 T 将字符串转换为 ISO 格式 + + // 获取各个部分 + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份要加1,并填充为2位 + const day = String(date.getDate()).padStart(2, '0'); // 填充为2位 + let hours = date.getHours(); + const minutes = String(date.getMinutes()).padStart(2, '0'); // 填充为2位 + const seconds = String(date.getSeconds()).padStart(2, '0'); // 填充为2位 + + // 计算12小时制的小时和 AM/PM + const ampm = hours >= 12 ? 'PM' : 'AM';//PM代表下午,AM代表上午。 + // hours = String(hours % 12 || 12).padStart(2, '0'); // 0转换为12 + + // 格式化输出 + return `${day}-${month}-${year} ${hours}:${minutes}:${seconds} ${ampm}`; +} /** * 发布日期的特定显示方式, @@ -254,10 +277,12 @@ Object.assign(Date, { time2Date, parseTime, pubTime, + convertTo12HourFormat }); export default{ time2Date, parseTime, - pubTime + pubTime, + convertTo12HourFormat } \ No newline at end of file diff --git a/src/views/contract/index.vue b/src/views/contract/index.vue index b69064d..f76e68a 100644 --- a/src/views/contract/index.vue +++ b/src/views/contract/index.vue @@ -101,7 +101,7 @@