学习目标:
学习目标
- 获取当前日期昨天、今天、明天的日期
学习内容:
内容
一、方法一
1. 获取当前日期
var today = new Date();
复制
2. 获取昨天的日期
var yesterday = this.getDay(-1)
复制
3. 获取今天的日期
var today = this.getDay(0)
复制
5. 获取明天的日期
var tomorrow = this.getDay(1)
复制
6. 调用的方法示例代码如下所示:
获取当前日期昨天、今天、明天的日期
methods: { getDay(day) { var today = new Date(); var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day; today.setTime(targetday_milliseconds); //注意,这行是关键代码 var tYear = today.getFullYear(); var tMonth = today.getMonth(); var tDate = today.getDate(); tMonth = this.doHandleMonth(tMonth + 1); tDate = this.doHandleMonth(tDate); return tYear + "-" + tMonth + "-" + tDate; }, doHandleMonth(month) { var m = month; if (month.toString().length == 1) { m = "0" + month; } return m; }, },
复制
二、方法二
1. 获取当前日期
var today = new Date();
复制
2. 获取昨天的日期
today .setTime(day1.getTime()-24*60*60*1000); var yesterday = today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();
复制
3. 获取今天的日期
today .setTime(today .getTime()); var day= today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();
复制
4. 获取明天的日期
today .setTime(today .getTime()+24*60*60*1000); var tomorrow= today .getFullYear()+"-" + (today .getMonth()+1) + "-" + today .getDate();
复制
总结:
知识小结:
总结:
- 获取当前日期并进行计算想要的日期
{ text: '本月', onClick(picker) { // 获取当前日期 const today = new Date(); // 获取当前月份的第一天 const start = new Date(today.getFullYear(), today.getMonth(), 1); // 获取当前月份的最后一天 const end = new Date(today.getFullYear(), today.getMonth() + 1, 0); picker.$emit('pick', [start, end]); } },
复制
- 1、传值调用此方法
created() { console.log("昨天:", this.getDay(-1)) console.log("今天:", this.getDay(0)) console.log("明天:", this.getDay(1)) console.log("20年以后:", this.getDay(20 * 365)) }
复制
- 获取当前时间, new Date()
- day为number,getDay(-1):昨天的日期;getDay(0):今天的日期;getDay(1):明天的日期;