2

I'm trying to check if the date entered in datetime field is a current or a future date. I've tried:

function validations(){
    var value=document.getElementById("showdate").value;
    if (new Date() > new Date(value)) {
        alert("Past date");
    }
}
<Form method="post" onsubmit="validations()" autocomplete>
    <input type="datetime-local" name="showdate" class="right" required="required" id="showdate">
    <input type="submit">
</form>

But this code will work only with the date field.

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
anchal saini
  • 105
  • 2
  • 9

2 Answers2

2

Generally, in JS, to compare dates you should try:

function isFutureDate(value) {
    d_now = new Date();
    d_inp = new Date(value)
    return d_now.getTime() <= d_inp.getTime();
}

Run regular comparison operators on the getTime function of a Date object.

Airwavezx
  • 323
  • 1
  • 9
  • Evaluation of the comparison will not invoke the`toString()` method like invoking `console.log()` does. It will get the base value of the `Date` object (which is a `Number` instance) to perform the comparison. There's no need to call `getTime()`. – Robby Cornelissen Aug 17 '17 at 08:49
  • @RobbyCornelissen, Yes you are correct, I deleted my comment. I am not sure what's wrong with comparing two date objects, but for all I know: `console.log(new Date() == new Date());` returns false while `console.log(new Date().getTime() == new Date().getTime());` returns true. – Airwavezx Aug 17 '17 at 08:53
  • `==` is an equality operator, `<=` is a relational operator, and the semantics differ. `console.log(new Date() <= new Date())` will yield `true`. – Robby Cornelissen Aug 17 '17 at 08:56
  • @RobbyCornelissen So? If a method is to be invoked when comparing objects (i.e. `valueOf()`) it will be invoked on both `==` and `<=` don't you think? – Airwavezx Aug 17 '17 at 08:58
  • @RobbyCornelissen then why isn't it possible to compare two Date objects in the manner OP described? – Airwavezx Aug 17 '17 at 09:02
  • @RobbyCornelissen Well, because you are comparing identical strings. In the same way `console.log('doge' <= 'doge')` yields `true`. – Airwavezx Aug 17 '17 at 09:04
  • If you're comparing identical strings, why does `new Date() == new Date()` return false then? Does 'doge' == 'doge' return false? – Robby Cornelissen Aug 17 '17 at 09:06
  • @RobbyCornelissen dunno, I'm gutted man. Can you provide an explanation? I honestly want to know. – Airwavezx Aug 17 '17 at 09:08
  • 2
    It's the difference between [abstract relational comparison](https://tc39.github.io/ecma262/#sec-abstract-relational-comparison) and [abstract equality comparison](https://tc39.github.io/ecma262/#sec-abstract-equality-comparison). Relational comparison includes a `toPrimitive()` conversion on both sides, equality comparison doesn't. – Robby Cornelissen Aug 17 '17 at 09:25
0

I believe, if you call the getTime() method of the date object, then it will return with the timestamp representation of the date object, so you will be able to compare the two datetimes better. Something like this:

function isFutureDate(value) {
    return new Date().getTime() <= new Date(value).getTime();
}
SaWo
  • 1,515
  • 2
  • 14
  • 32
  • Same question as for the other answer: what's wrong with directly comparing date objects? You state *you will be able to compare the two datetimes better*. Better how? – Robby Cornelissen Aug 17 '17 at 08:53
  • See [*Compare two dates with JavaScript*](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript). – RobG Aug 17 '17 at 23:24