uniapp怎么把time 2024-04-15T152455000+0800转换成 time 2024-04-15 152455
在UniApp中,可以使用JavaScript的Date对象来进行日期和时间的转换。下面是一个将时间字符串转换为指定格式的示例:
// 原始时间字符串
let time = '2024-04-15T15:24:55.000+08:00';
// 将时间字符串转换为Date对象
let dateObj = new Date(time);
// 获取年、月、日、时、分、秒
let year = dateObj.getFullYear();
let month = dateObj.getMonth() + 1;
let day = dateObj.getDate();
let hour = dateObj.getHours();
let minute = dateObj.getMinutes();
let second = dateObj.getSeconds();
// 格式化为指定格式
let formattedTime = `${year}-${month < 10 ? '0' + month : month}-${day < 10 ? '0' + day : day} ${hour < 10 ? '0' + hour : hour}:${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`;
console.log(formattedTime);
在上述示例中,首先将时间字符串转换为Date对象,然后使用Date对象的方法获取年、月、日、时、分、秒的值。最后,使用字符串模板将这些值按照指定格式拼接起来。最终输出的结果就是转换后的时间格式为time: 2024-04-15 15:24:55
原文地址: https://www.cveoy.top/t/topic/iFnb 著作权归作者所有。请勿转载和采集!