0

I need to validate whether the date mentioned should be greater than or equal to the current date. But problem here is if user is giving old date '03/11/0018', by default the year gets considered as 2018(as per the below code). One way which I did is to take the input data and get the year using substring and handle(not mentioned in the below code). Is there any other way to handle this?

    <html>
    <head>
    <script>
    function dateCompare() {
       var d1 = new Date('03/11/0018'); 
       var d2 = new Date('03/11/2018'); //consider today's date is 03/11/2018
       if(d1 < d2){
           //validate the input date
           document.write("entered date cannot be earlier to the current date");
       }
       else{
           //validation passed
           document.write("Ideally 03/11/0018 is earlier than 03/11/2018 & validation should be failed");
       }
    }
    dateCompare();
    </script>
    </head>
    </html>
Ganesh
  • 21
  • 7

2 Answers2

5

MDN strongly discourages the use of new Date(string) or Date.parse(string) because the implementation is browser-dependent -- and as you've found, often somewhat unintuitive.

You can parse the string yourself, and it's good to do so especially when the format is well defined.

const re = /(\d\d)\/(\d\d)\/(\d\d\d\d)/;

const [whole, day, month, year] = re.exec('03/02/2018');

Then if you want to, you can use these numbers to construct a Date:

const d = new Date(year, month - 1, day);

Note that the Date constructor expects zero-indexed months (that is, January is 0).

Also note that Date maps years 0 - 99 to 1900 - 1999. If you don't want this, you'll need to force it with Date.prototype.setFullYear and Date.prototype.getFullYear. Where this is necessary, or not, isn't very consistent:

const d = new Date(88, 1, 2);
console.log(d); // 1988-02-02T00:00:00.000Z

d.setFullYear(88);

console.log(d); // 0088-02-02T00:00:00.000Z
console.log(d.getYear()); // -1812
console.log(d.getFullYear()); // 88

There are enough pitfalls here that you should consider using a library with a cleaner date API.

slim
  • 40,215
  • 13
  • 94
  • 127
  • Library recommendations don't belong in the answer, but `moment` is a popular one. – slim Mar 11 '18 at 12:23
  • Have you considered using ISO 8601 format? – connexo Mar 11 '18 at 12:29
  • 1
    @connexo I would always prefer to use ISO8601 when encoding dates as strings, but OP has a different input format. It wouldn't really change the answer -- the MDN recommendation against `new Date(string)` doesn't have an exception for ISO8601. One of the problems with the parse method is that it's liberal about its input format, so there's scope for it to make mistakes. – slim Mar 11 '18 at 12:33
  • That's why W3C has defined ISO 8601 for date formats in Javascript. If you deliberately pass invalid arguments, ignoring the standard API, you place yourself at the client vendor's mercy. – connexo Mar 11 '18 at 12:35
  • @connexo—the W3C is not involved in the ECMA-262 standard. Despite using ISO 8601 formats, ECMA-262 is inconsistent with it for parsing. More grist for the "don't use the built-in parser" mill. ;-) – RobG Mar 11 '18 at 23:07
-1

You need to pass date strings in ISO 8601 format yyyy-mm-dd to the Date constructor:

function dateCompare() {
  var d1 = new Date('0018-03-11');
  var d2 = new Date('2018-03-11'); //assuming today's date is 03/11/2018
  if (d1 < d2) {
    //validate the input date
    console.log(`${d1} is earlier than ${d2}`);
  } else {
    //validation passed
    console.log("validation passed");
  }
}
dateCompare();
connexo
  • 53,704
  • 14
  • 91
  • 128
  • Using the built-in parser is rarely a good idea. ISO 8601 dates are parsed as UTC, so "work" in the case of simple comparison but not in others. – RobG Mar 11 '18 at 23:04