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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

Edited by Miggy64
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.