前端 js 日期格式化

93 阅读1分钟
date=Thu Dec 28 2023 16:44:25 GMT+0800 (中国标准时间)

function formatDate(date) { const year = date.getFullYear(); const month = ('0' + (date.getMonth() + 1)).slice(-2); const day = ('0' + date.getDate()).slice(-2); const hour = ('0' + date.getHours()).slice(-2); const minute = ('0' + date.getMinutes()).slice(-2); const second = ('0' + date.getSeconds()).slice(-2); return { yd: year{year}-{month}-day{day} {hour}:minute:{minute}:{second}, hs: hour:{hour}:{minute}:${second} } }