简体   繁体   中英

How to find if date if before current date in javascript

Dates are stored in javascript as yyyy-mm-dd strings whose time part is always 0 like

todays date is

"2016-09-04T00:00:00"

How to find if date is before current date ? I tried

alert(new Date("2016-09-04T00:00:00") < new Date());

but this returns true with is wrong if today is 2016-09-04

new Date() returns time also and this produces wrong result.

How to compare date part only ?

Should todays date converted to string and substring used to compare only first 10 characters of strings or is there something better ?

I read answers from referenced question but havent found clear answer to this question. Those answers describe generic solution for date equality comparison. This questions asks how to find if date part is before current date.

You are missing the timezone offset. Try this: https://jsfiddle.net/WalterIT/ved5a3hp/

dateA = new Date("2016-09-05T00:00:00");
offset = dateA.getTimezoneOffset() * 1000 * 60;
dateA = dateA.getTime() + offset;
dateB = new Date().setHours(0,0,0,0);

I've changed it to the 5th (it's already this day in my timezone. Please amend it, if necessary)

Try

var old = new Date("2017-09-04T00:00:00").getTime();
var curr  = Date.now();


alert((old < curr));

And change the year to test it.

Try dates.compare(a,b) - dates is a object. Returns a number. -1, 0 or 1.

Parsing if strings with the Date constructor is not recommended due to variances between browsers.

In this case, browsers conforming with ES5 and later will parse "2016-09-04T00:00:00" as a "local" date and time for midnight at the start of 4 September, 2016. Some browsers (like Safari) will incorrectly treat it as UTC. See above advice.

However, in all browsers, new Date() will create a Date for the current instant, not midnight at the start of the day. So if you run this code on 4 September 2016 then unless you run it at exactly 00:00:00.000 local time the time of new Date() will be after 00:00:00.000.

If you want to compare the two, then set the time part of new Date() to midnight at the start of the day, eg

 // Returns false on 5 September, 2016 local time console.log(new Date('2016-09-05T00:00:00') < new Date().setHours(0,0,0,0)); 

The above works in most browsers because the < operator coerces the first Date to its internal time value, and the return from setHours is the adjusted time value of the second Date.

Note

The host system time zone offset should be used in the creation of both dates, so has an equal effect on both so can be ignored in this case.

It is strongly recommended that you do not rely on parsing strings with the Date constructor or Date.parse (they are equivalent for parsing). Always manually parse strings. Use a bespoke function for the format you need to deal with, or use a library and provide the format to parse. That is the only way that you can be sure of correct parsing.

在日期对象上使用Gettime函数而不是对其进行比较。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM