Uniapp 日期格式转换: 将 'Mon Jul 31 2023 11:11:35 GMT+0800 (中国标准时间)' 转换为 '2023-07-31 11:11:35'
你可以使用 JavaScript 的 Date 对象来处理日期和时间的转换。以下是将给定日期字符串转换为特定格式的代码示例:
// 给定日期字符串
const dateString = 'Mon Jul 31 2023 11:11:35 GMT+0800 (中国标准时间)';
// 创建 Date 对象
const date = new Date(dateString);
// 提取所需的日期和时间信息
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
// 格式化日期和时间
const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
console.log(formattedDate); // 输出:2023-07-31 11:11:35
在上面的代码中,我们首先创建了一个 Date 对象,然后使用 getFullYear()、getMonth()、getDate()、getHours()、getMinutes() 和 getSeconds() 方法提取日期和时间的各个部分。然后,我们使用 padStart() 方法将各个部分补零,确保它们都具有两位数的格式。最后,我们将日期和时间部分组合成所需的格式,并将其打印到控制台上。
原文地址: https://www.cveoy.top/t/topic/p6Ls 著作权归作者所有。请勿转载和采集!