简体   繁体   English

在 JavaScript 中确定日期是否为今天的最佳方法是什么?

[英]What is the best way to determine if a date is today in JavaScript?

I have a date object in JavaScript and I want to figure out if that date is today.我在 JavaScript 中有一个日期对象,我想确定该日期是否是今天。 What is the fastest way of doing this?这样做的最快方法是什么?

My concern was around comparing date object as one might have a different time than another but any time on today's date should return true .我担心的是比较日期对象,因为一个人的时间可能与另一个不同,但今天日期的任何时间都应该返回true

You could use toDateString :您可以使用toDateString

var d = new Date()
var bool = (d.toDateString() === otherDate.toDateString());

The answers based on toDateString() will work I think, but I personally would avoid them since they basically ask the wrong question.我认为基于toDateString()的答案会起作用,但我个人会避免使用它们,因为它们基本上问错了问题。

Here is a simple implementation:这是一个简单的实现:

function areSameDate(d1, d2) {
    return d1.getFullYear() == d2.getFullYear()
        && d1.getMonth() == d2.getMonth()
        && d1.getDate() == d2.getDate();
}

MDN has a decent overview of the JS Date object API if this isn't quite what you need.如果这不是您所需要的, MDN对 JS 日期对象 API 有一个不错的概述。

var someDate = new Date("6 Dec 2011").toDateString();
var today = new Date().toDateString();
var datesAreSame = (today === someDate);

If both are Date() objects, you can use this to 'format' the date in a way that it will only compare on the year/month/day: if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0));如果两者都是 Date() 对象,则可以使用它来“格式化”日期,使其仅在年/月/日上进行比较: if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0));

Nearly identical question: How to check if input date is equal to today's date?几乎相同的问题: 如何检查输入日期是否等于今天的日期?

我更喜欢使用时刻库

moment('dd/mm/yyyy').isSame(Date.now(), 'day');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 JavaScript 日期初始化为午夜的最佳方法是什么? - What is the best way to initialize a JavaScript Date to midnight? 用 JavaScript 确定一个月天数的最佳方法是什么? - What is the best way to determine the number of days in a month with JavaScript? 在 JavaScript 中确定 URL 是否属于当前域的最佳方法是什么? - What is the best way to determine if a URL belongs to the current domain in JavaScript? 在rails中保持javascript库最新的最佳方法是什么? - What is the best way to keep javascript libraries up-to-date in rails? 在Javascript中计算日期差异的最佳方法是什么 - What's the best way to calculate date difference in Javascript 在javascript中比较日期对象的时间组件的最佳方法是什么 - What is the best way of comparing the time component of a date object in javascript 将日期从客户端(Javascript)发送到WebApi的最佳方法是什么 - What is the best way to send Date from client(Javascript) to WebApi 在 JavaScript 中获取日期值以在 MongoDB 中发送的最佳方法是什么? - What is The Best way to get date value in JavaScript to send in MongoDB.? 准确计算今天和另一个日期之间时间的最佳方法? - Best way to accurately calculate the time between Today and another date? 将格式为YYYYMMDD的JavaScript字符串中的日期转换为JavaScript日期的最佳方法是什么? - What is the best way to convert date from JavaScript string in format YYYYMMDD to JavaScript date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM