js常用

一、格式化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 }

 

posted @ 2023-12-11 15:20  record-100  阅读(15)  评论(0)    收藏  举报