TypeScript获取格式化日期

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012760435/article/details/84639247

代码挺简单的,但是写出来却花费了不少功夫,主要还是不适应ts的类型机制。记下来备忘。

static getNowDate(): string {
  const date = new Date();
  let month: string | number = date.getMonth() + 1;
  let strDate: string | number = date.getDate();

  if (month <= 9) {
    month = "0" + month;
  }

  if (strDate <= 9) {
    strDate = "0" + strDate;
  }

  return date.getFullYear() + "-" + month + "-" + strDate + " "
  + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}

猜你喜欢

转载自blog.csdn.net/u012760435/article/details/84639247