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秒