- 获取当前时间
代码:
const today = new Date();
const year = today.getFullYear();
const month = (today.getMonth() + 1).toString().padStart(2, '0');
const day = today.getDate().toString().padStart(2, '0');
const formattedDate = `${year}-${month}-${day}`;
- 获取前一天时间
代码:
const now = new Date(); // 获取当前时间
const yesterday = new Date(now.getTime() - 24 * 60 * 60 * 1000); // 获取前一天的时间
// 拼接成指定格式的字符串
类型一:例如:1-9号的时候:2023-11-1(若需要反显在Element组件上,这种会有问题,反显不出来)
Const yesterdayStr =
`${yesterday.getFullYear()}-${yesterday.getMonth() + 1}-${yesterday.getDate()}`;
类型二:例如:1-9号的时候:2023-11-01
const yesterdayStr =
`${yesterday.getFullYear()}-${(yesterday.getMonth()+1).toString().padStart(2,'0')}-${yesterday.getDate().toString().padStart(2, '0')}`;