0

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.

Andrus
  • 26,339
  • 60
  • 204
  • 378
  • 2
    Possible duplicate of [Compare two dates with JavaScript](http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – rlemon Sep 04 '16 at 20:39
  • I read those answers but havent found clear answer to my question. Can you provide more information, which answer in referenced question answers directly to this question ? – Andrus Sep 04 '16 at 20:41

5 Answers5

1

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)

Michael Walter
  • 1,427
  • 12
  • 28
  • THe timezone offset has nothing to do with it because, in this case, both are treated as local. The OP returns true because after the first millisecond on a particular date, `new Date()` will return a Date for a later instant than 00:00:00 on that date. – RobG Sep 05 '16 at 01:02
  • @RobG Did you check my fiddle link? Only when I add/remove the offset, I get the correct time stamp. Without that, the time stamp of dateA is always two hours ahead (I am in GMT +2). I give you one example of your answer and my answer in comparison. Please check: https://jsfiddle.net/WalterIT/ved5a3hp/2/ – Michael Walter Sep 05 '16 at 11:03
  • Please don't post elsewhere, you can post code here as a runnable snippet. If parsed per ECMA-262, "2016-09-05T00:00:00" will produce a date for 00:00:00 in the host system time zone. If your time zone is UTC+0200, your adjustment sets the date to 2016-09-04T22:00:00 in that time zone (i.e. the date is now the previous day) and *dateA* will be before *dateB* as is shown in your fiddle. It produces the wrong result (i.e. true instead of false). For users west of GMT it will have the opposite effect. – RobG Sep 05 '16 at 22:25
0

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.

Caspar Wylie
  • 2,818
  • 3
  • 18
  • 32
  • Your answer is about full timestamp comparison. I need to compare date part of string "2016-09-04T00:00:00" with todays date. How to do this ? – Andrus Sep 04 '16 at 20:46
  • you need to convert it to a timestamp in order to compare them as integers. It does what you want, just differently. – Caspar Wylie Sep 04 '16 at 20:47
  • `alert(new Date("2016-09-04").getTime() < Date.now())` outputs `true` which is wrong. Since today is 2016-09-04 result must be false – Andrus Sep 04 '16 at 20:51
  • ...or if you want it the other way round , change < to > – Caspar Wylie Sep 04 '16 at 20:53
  • Using edited answer `alert(new Date("2016-09-04T00:00:00").getTime() < Date.now())` also output wrong result, true. How to compate dates only, ignoring time? – Andrus Sep 04 '16 at 20:53
  • you are getting mixed up. Change < to > then...if you want it the other way round. Got it? – Caspar Wylie Sep 04 '16 at 20:55
  • I dont understand this comment. If date part is 2016-09-04 this date must be equal to current date, both < and > operators must return `false` – Andrus Sep 04 '16 at 20:56
  • Then as I said earlier, specify a default time, like 00:00:00. You must. Otherwise the timestamps will never be accurate to a date, only a time. – Caspar Wylie Sep 04 '16 at 20:58
  • Or just write some code that makes sure the day specified isn't the current day... – Caspar Wylie Sep 04 '16 at 21:00
  • Dates in code are coming from json and are strings with time always `T00:00:00` How to get current date with same time part, `T00:00:00` ? – Andrus Sep 04 '16 at 21:06
0

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

  • I need to compare date parts only. Time part in date to be compared is T00:00:00 always, How to get current date with time part T00:00:00 for comparison ? – Andrus Sep 04 '16 at 21:09
  • 1
    To get rid of the hours, minutes, seconds and milliseconds on date1 do the following: date1.setHours(0,0,0,0) – Michael B Hildebrand Sep 04 '16 at 21:17
  • `console.log(new Date("2016-09-05T00:00:00") === new Date().setHours(0, 0, 0, 0))` and `console.log(new Date("2016-09-05T00:00:00").getTime() === new Date().setHours(0, 0, 0, 0)); ` both output false. So it looks like setHours is not working – Andrus Sep 04 '16 at 21:31
  • I thought'false' was what you were looking for. Today is the 4th. – Michael B Hildebrand Sep 04 '16 at 21:38
  • @Andrus—`new Date("2016-09-05T00:00:00")` returns a Date object that is only ever `===` to itself. `new Date("2016-09-05T00:00:00")` **should** be treated as local, but not all browsers do that. – RobG Sep 05 '16 at 01:20
-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, e.g.

// 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.

RobG
  • 142,382
  • 31
  • 172
  • 209
-3

Use the Gettime function on the date objects than compare them.

  • I need to compare date part of string "2016-09-04T00:00:00" with todays date. Your answer is about full timestamp comparison – Andrus Sep 04 '16 at 20:45