判断函数:
isInToday(date){
let currentDay = new Date().getDate()
let transfromDay = new Date(date).getDate()
console.log(transfromDay) // 15
return currentDay===transfromDay
},
调用函数:
// let date = ""+new Date()
// console.log(date) // Tue Oct 15 2019 22:14:42 GMT+0800 (中国标准时间)
// console.log(typeof date) // string
// console.log(this.isInToday(date)); // true
// let date = '2019-10-15 12:35:45'
// console.log(this.isInToday(date)) // true
// let date = '2019/10/15 12:35:45'
// console.log(this.isInToday(date)) // true
let date = '2019/10/15'
console.log(this.isInToday(date)) // true
从上可以看出以后对时间的计算只要是这几种格式的都是对的
判断是否是今天上个方法有缺陷,没考虑年月
正确的写法是这样的:
created(){
console.log("在今天?",this.isInToday("2019/10/20 11:07:57"));
console.log("在今天?",this.isInToday("2019/10/10 11:07:57"));
},
methods:{
isInToday(dataStr){
// 使用此方法的前提是一定要知道传递过来的时间格式是啥
// 本例的为"2019/10/20 11:07:57"
let transformDate = dataStr.split(" ")[0]
let year = new Date().getFullYear()
let mounth = new Date().getMonth()+1
let day = new Date().getDate()
let nowDate = year+"/"+mounth+"/"+day
console.log(transformDate,nowDate)
return transformDate === nowDate
},
}