1.使用js获取年月日

 const getYMD = () => {  
  let myDate = new Date()
  let myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
  let myMonth = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)
  let myToday = myDate.getDate(); //获取当前日(1-31)
  myMonth = myMonth > 9 ? myMonth : '0' + myMonth
  myToday = myToday > 9 ? myToday : '0' + myToday
  let nowDate = myYear +  myMonth +  myToday
  return nowDate
 }

2.使用moment格式化日期

 (1)下载moment模块

    npm install moment

  (2)导入moment

  import moment from 'moment'

 (3)使用 moment 

1. moment().format('YYYY-MM-DD HH:mm:ss');  //获取当年月日时分秒 2022-09-26 14:05:59

2. moment().format('YYYY年MM月DD日);  //获取当年月日  2022年09月26日

注意:format 是你要显示得日期格式 YYYY年 MM月 DD日 HH时 mm分 ss秒

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐