判断输入字符串格式的时间是否是今天

37 篇文章 0 订阅

判断函数: 

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
  },

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值