MockY Posted August 6, 2014 Share Posted August 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290314-date-check-two-dates-must-be-a-year-apart/ Share on other sites More sharing options...
Barand Posted August 6, 2014 Share Posted August 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290314-date-check-two-dates-must-be-a-year-apart/#findComment-1487042 Share on other sites More sharing options...
MockY Posted August 6, 2014 Author Share Posted August 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/290314-date-check-two-dates-must-be-a-year-apart/#findComment-1487044 Share on other sites More sharing options...
Miggy64 Posted August 7, 2014 Share Posted August 7, 2014 (edited) 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. Edited August 7, 2014 by Miggy64 Quote Link to comment https://forums.phpfreaks.com/topic/290314-date-check-two-dates-must-be-a-year-apart/#findComment-1487085 Share on other sites More sharing options...
MockY Posted August 7, 2014 Author Share Posted August 7, 2014 Logical/necessary or not, this is still something I want in place Quote Link to comment https://forums.phpfreaks.com/topic/290314-date-check-two-dates-must-be-a-year-apart/#findComment-1487090 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.