Jump to content

Date check - Two dates must be a year apart


MockY

Recommended Posts

I have two text fields in a form. Both accepts dates. I'd like to check whether the second date provided is exactly one year in the future from the first provided date.

I can easily do this in PHP but since I need to do the check once the user leaves the second field, I have to resort to JavaScript, using onblur or something.

 

There are two issues I have.

1. Make sure that the strings provided are indeed dates (I would use strtodate() in PHP for this)

2. Regardless of month provided, making sure that the second date is exactly a year in the future of the already provided year

 

I've only captured the values and passed them to Date(). However, it can only handle european style, and if I enter 1981-06-16, the value returned is Jun 15 1981 17:00:00 which is obviously wrong.

 

Here is the simple broken code I have

var text1 = document.getElementById("text1");
var start_date = new Date(text1.value);
    
 var text2 = document.getElementById("text2");
 var end_date = new Date(text2.value);
    
 alert(start_date);

I need some guidance.

It is allowed to enter any date, but I want the user to be notified if the date they enter is not a year in the future. 99% of the times, that is what will be entered, but once in a blue moon, a different date is required.

That only happens every 4 years if you're being that exact, but I can't think of anything that needs to be that exact. :happy-04:

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.