vue 获取昨天 今天 明天的年月日
时间: 2023-09-10 17:15:04 浏览: 138
可以使用 JavaScript 的 Date 对象来获取昨天、今天和明天的年月日,然后在 Vue 中使用。
以下是一个获取昨天、今天和明天年月日的示例代码:
```js
// 获取当前时间
const today = new Date();
// 获取昨天的时间
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
// 获取明天的时间
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
// 格式化日期为 YYYY-MM-DD
const formatDate = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
};
// 在 Vue 中使用
new Vue({
data: {
today: formatDate(today),
yesterday: formatDate(yesterday),
tomorrow: formatDate(tomorrow)
}
});
```
这样就可以在 Vue 中使用 `today`、`yesterday` 和 `tomorrow` 来获取对应的年月日了。