Jump to content


Photo

Date check - Two dates must be a year apart


  • Please log in to reply
4 replies to this topic

#1 MockY

MockY

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts
  • LocationSacramento

Posted 06 August 2014 - 01:20 PM

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.



#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,768 posts
  • LocationCheshire, UK

Posted 06 August 2014 - 03:05 PM

If the second must always be exactly one year after the first date then why ask the user to enter a second date? Just have them enter one date then add the year yourself for the second.


moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 MockY

MockY

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts
  • LocationSacramento

Posted 06 August 2014 - 03:15 PM

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.



#4 Miggy64

Miggy64

    Newbie

  • Members
  • Pip
  • 9 posts
  • LocationLivonia, MI

Posted 07 August 2014 - 08:19 AM

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, 07 August 2014 - 08:19 AM.


#5 MockY

MockY

    Advanced Member

  • Members
  • PipPipPip
  • 75 posts
  • LocationSacramento

Posted 07 August 2014 - 09:15 AM

Logical/necessary or not, this is still something I want in place  :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com