0

I need to validate a date for if it is a future valid date. While inputting date in the form I'm using the date format dd/mm/yyyy using Jquery validation plugin with dateITA rule. I've added the following validation method

 $.validator.methods.futuredocexp = function( value, element ) {    
    var now = new Date();
    var myDate = new Date(value);
    return this.optional(element) || myDate > now;    
 };

It only works if dates are in the american format like dd-mm-yyyy How can I make it work for dd/mm/yyyy?

Om Sao
  • 7,064
  • 2
  • 47
  • 61
pippo
  • 183
  • 1
  • 5
  • 18
  • Try `value.split("/").join("-")` – Abhi Oct 04 '17 at 11:19
  • Hi thanks for your help. I've tried it but doesn't work i'm afraid – pippo Oct 04 '17 at 11:26
  • Where did you try this? And this fix is specific to date format with "/" – Abhi Oct 04 '17 at 11:29
  • `dd-mm-yyyy` is not American date format. Also, do you mean that you are taking date input in format `dd/mm/yyyy` always and you have date in String format ? – Om Sao Oct 04 '17 at 11:29
  • 1
    Hi, i've tried in here: var myDate = new Date(value.split("/").join("-")); yes the string format is dd/mm/yyyy – pippo Oct 04 '17 at 11:30
  • 1
    @pippo: split and join is working, what exactly is the issue, can you please tell. See the answer. – Om Sao Oct 04 '17 at 11:48
  • Hi @OmSao thanks for your answer the problem is that even if the date is in the future jquery validation still says that is not valid. – pippo Oct 04 '17 at 11:49
  • @pippo: You don't need to use jquery validation, I updated my answer. Please refer, it will return true/false based on future date test. – Om Sao Oct 04 '17 at 12:06
  • Hi i've tried your method and console.log() return Input dateString was: 20/09/1984 Date Object is: Invalid Date Is input dateStr a future date ?? jfalse – pippo Oct 04 '17 at 12:19
  • This is because you are giving input in `dd/mm/yyyy` format, which is not US(American format). Correct format for date input is `mm/dd/yyyy` format which I have shown in example. If you want to give input strictly for `dd/mm/yyyy` format then, you need to swap 1st and 2nd element of splitted array. – Om Sao Oct 04 '17 at 12:23
  • Please check the answer now. – Om Sao Oct 04 '17 at 12:32
  • I need to use it with jquery validation – pippo Oct 04 '17 at 12:41

1 Answers1

0

Splitting the dateString with \ and then joining with - will do. Here is the example:

var dateStringwithFwdSlash1 = "21/04/2050";  //  dd/mm/yyyy format
var dateStringwithFwdSlash2 = "21/04/2005";  //  dd/mm/yyyy format

Array.prototype.swapItems = function(a, b){
    this[a] = this.splice(b, 1, this[a])[0];
    return this;
}

function isItFutureDate(dateStr){
   var d = new Date(dateStr.split("/").swapItems(0,1).join("-"));
   var now = new Date();
   console.log("Input dateString was: "+dateStr+" \t Date Object is: " +d);
   console.log('Is input dateStr a future date ??');  // .getTime() returns milliseconds since 1 Jan 1970
   console.log((d.getTime()-now.getTime()) > 0 );
}


isItFutureDate(dateStringwithFwdSlash1);
isItFutureDate(dateStringwithFwdSlash2);
Om Sao
  • 7,064
  • 2
  • 47
  • 61
  • It doesn't make any sense to parse a string, reformat it, then give it to another parser to create a Date. Why not just parse the string and pass the values directly to the Date constructor? That way you avoid the built-in parser entirely and save an (unreliable) parse? – RobG Oct 04 '17 at 13:10
  • @RobG: You can't pass date string to `Date()` in `dd-mm-yyyy` format, you have to convert it to format `mm-dd-yyyy` for Date constructor to consume. How, you would do that ? Please tell. – Om Sao Oct 04 '17 at 13:14
  • Having done `dateStr.split("/")` you now have the arguments you need to call the Date constructor directly. It comes done to a [two line function](https://stackoverflow.com/questions/28639009/compare-and-filter-data-with-date-in-mm-dd-yyyy-format-from-current-date-using-u/28639241#28639241). – RobG Oct 04 '17 at 22:43
  • You are right. It didn't come to my mind :-(. – Om Sao Oct 05 '17 at 01:54