HLJ 发布于
2018-11-09 09:49:05

如何在JAVASCRIPT中确定日期是否为今天

如何确定JavaScript Date对象实例是否代表“今天”的日期/时间?
给定一个日期实例,我们可以使用getDate(),getMonth()并且getFullYear()方法,它会返回一个日期的日,月,年,并将它们与今天,可以使用被检索new Date()。
这是一个很小的函数,如果参数是今天,则返回true。
const isToday = (someDate) => {
  const today = new Date()
  return someDate.getDate() == today.getDate() &&
    someDate.getMonth() == today.getMonth() &&
    someDate.getFullYear() == today.getFullYear()
}
你可以像这样使用它:
const today = isToday(myDate)
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2018-11-09/300.html
最后生成于 2023-06-18 18:31:40
此内容有帮助 ?
0