日期格式化为yyyy-MM-dd hh:mm:ss,日期格式化函数的封装,获取当前日期

 1.获取当前日期,并格式化为yyyy-MM-dd hh:mm:ss

getNowDate(){
      const date= new Date();
      const year= date.getFullYear();
      const month= (date.getMonth() + 1).toString().padStart(2, '0');
      const day= date.getDate().toString().padStart(2, '0');
      const hour= date.getHours().toString().padStart(2, '0');
      const minute= date.getMinutes().toString().padStart(2, '0');
      const second= date.getSeconds().toString().padStart(2, '0');
      const nowDate= `${year}-${month}-${day} ${hour}:${minute}:${second}`
      console.log("nowDate", nowDate); //nowDate 2024-08-15 10:29:23
      return nowDate
},

2.yyyy-MM-dd hh:mm:ss、yyyy-MM-dd和yyyy-MM-dd hh:mm格式的日期都转换成yyyy-MM-dd的日期格式

clearTime(date) {
      if (!!date) {
        if (date.length > 11) {
          console.log("date1",date);
          console.log("$$",date.replace(/^(\d{4})-(\d{2})-(\d{2})\s(\d{0,2}):{0,1}(\d{0,2}):{0,1}(\d{0,2})$/, "$1-$2-$3"));
          
          return date.replace(/^(\d{4})-(\d{2})-(\d{2})\s(\d{0,2}):{0,1}(\d{0,2}):{0,1}(\d{0,2})$/, "$1-$2-$3")
        } else {
          console.log("date2",date);
          return date
        }
      }
    },
let date= "2024-08-10 12:12:12"
let _date= "2024-08-10"
let __date= "2024-08-10 12:12"
clearTime(date)
clearTime(_date)
clearTime(__date)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值