js常用

发布时间 2023-12-11 15:21:57作者: record-100

一、格式化Date类型为字符串

1 formatDate(date) {
2   const year = date.getFullYear();
3   const month = (date.getMonth() + 1).toString().padStart(2, '0');
4   const day = date.getDate().toString().padStart(2, '0');
5   const hours = date.getHours().toString().padStart(2, '0');
6   const minutes = date.getMinutes().toString().padStart(2, '0');
7   const seconds = date.getSeconds().toString().padStart(2, '0');
8   return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
9 }