Hiya all.
I have a form which the user selects a date from a javascript style calendar, it puts the date into a field in the format dd/mm/yyyy.
I want the form to compare this user selected date to a date which it will work out, which is today's date pluys 7 days.
The reason is, the field is a 'Required by' field on a form and I dont want the user to enter a date in the past or one which is less than 7 days from today.
So it will comare the date entered to a date 7 days from today.
I have the logic right (I think), and included it here, but for somereason a date where the day is 10th or greater seems to trips up!
Any ideas??? - <script>
-
function checkadate(){
-
-
//This bit gets todays date and add seven days
-
var currentdate = new Date();
-
currentdate.setDate(currentdate.getDate()+7);
-
var day = currentdate.getDate();
-
var month = currentdate.getMonth() + 1;
-
var year = currentdate.getFullYear();
-
-
//This bit puts that date in to dd/mm/yyyy format
-
var todat = (day + "/" + month + "/" + year)
-
-
alert("Todays date plus 7 is " +todat);
-
-
//This is the date entered by the user.
-
var ReqDate = document.getElementById("DateRequired").value;
-
-
alert("Selected date is " + ReqDate);
-
-
var SDate = (todat);
-
var EDate = document.getElementById("DateRequired").value;
-
var endDate = new Date(EDate);
-
var startDate= new Date(SDate);
-
-
-
if(startDate > endDate){
-
alert("Date must be at least 5 working days in advance to allow for processing.\nThe earliest date available is " + SDate);
-
document.getElementById("DateRequired").value = SDate;
-
return false;
-
}else{
-
}
-
-
-
-
}
-
</script>
7 1629
You already have currentdate as a Date object for the start date. Create another date object using the user inputted date via the setFullYear method, then compare the two.
Pardon my ignorance, but I'm not quite following....... :/
On lines 5-6, you created a date object and set the date.
One line 17, you have the date value for the end date. Split the string using str.split("/") to get an array of date/month/year values. Then create a new Date object and use setFullYear() to set the date.
Now you have two date objects ready for comparing.
Again, I can see the logic, but there must be a problem with my date formatting, I'm now getting NaN on the third alert box - when trying to setFullYear on ReqDate.
I've modified and simplified the script, it was over kill last time, but it's still not working it out. Does the setDate getDate put it into dd/mm/yyyy or american format?
The value I'm bringing in from the form is in dd/mm/yyyy, would that cause a problem?
I've included updated code....
Thanks in anticipation... - <script>
-
function checkadate(){
-
-
//Gets todays date and add seven days.
-
var currentdate = new Date();
-
currentdate.setDate(currentdate.getDate()+7);
-
alert(currentdate);
-
-
-
//Gets date entered
-
var ReqDate = document.getElementById("DateRequired").value;
-
-
//Splits date into an array
-
var ReqArray=ReqDate.split("/");
-
alert(ReqArray);
-
-
var ReqDate = new Date();
-
ReqDate.setFullYear(ReqArray);
-
alert(ReqDate);
-
//**Failing here**
-
-
-
if(ReqDate > currentdate){
-
alert("Date must be at least 5 working days in advance to allow for processing.\nThe earliest date available is " + SDate);
-
document.getElementById("DateRequired").value = SDate;
-
return false;}
-
-
}
-
</script>
Use the array values separately and see how setFullYear is also used (with three parameters). - //Gets date entered
-
var ReqDate = document.getElementById("DateRequired").value;
-
-
//Splits date into an array
-
var ReqArray=ReqDate.split("/");
-
-
var ReqDate = new Date();
-
ReqDate.setFullYear(ReqArray[2],ReqArray[1]-1,ReqArray[0]);
-
Brilliant!! Got it working, many thanks for your xcellent help as usual!
I've posted the working code for anyone else to use in the future, as it took me AGES to get working what on reflection is a simple task.
Jut change the params as you see fit.
< - script>
-
function comparetwodates(){
-
//Gets todays date and add seven days.
-
var currentdate = new Date();
-
currentdate.setDate(currentdate.getDate()+7);
-
-
//Gets date entered
-
var myDate = document.getElementById("mydate").value;
-
-
//Splits date into three components
-
var ReqArray=myDate.split("/"); //Or change the '/' for your date seperator
-
-
//Pieces date back as a date
-
var myDate = new Date();
-
ReqDate.setFullYear(ReqArray[2],ReqArray[1]-1,ReqArray[0]);
-
-
-
//Checks to see if myDate is earlier than currentdate
-
if(ReqDate < currentdate){
-
-
alert("My Date is less than 7 days from today.");
-
return false;
-
}
-
}
-
</script>
Yes, you just need to know how dates work in JavaScript. You may find this link useful (if you haven't already seen it) and the accompanying reference.
Anyway, glad to hear that you've got it working.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Gav |
last post by:
I have a database with date of births stored dd/mm/yyyy (english dating
system) and =date() returns a date in the same format in my server.
how do i find the persons age using these two pieces of...
|
by: Paolo |
last post by:
Friends,
I have created a database in Access 97 based on two splitted files.
This application has been running and used by a couple of hundreds of
users in different Offices around the World. I...
|
by: David B |
last post by:
I have a number of queries, running one after the other, which do quite a
complex calculation. A text box on a form provides the date for this routine.
I have another routine I wish to do and it...
|
by: John Feeley |
last post by:
am tring to add a number of years to a dob. im doing this by adding my
date+years*365.26 I get a string of numbers. I then convert the number in
the next column to actual date again. I'm getting...
|
by: Julie Wardlow |
last post by:
Help!
I am calculating a future date using the DateAdd function in a query (the
calculation also involves an IIf statement), and have managed to get this
formula to produce the required result....
|
by: Issac Gomez |
last post by:
IN VB.Net, you can use the DateDiff method to calculate the difference between 2 days. How do you do this in C#? I want to find the number of days between 2 dates. I there a 0 days, then I want to...
|
by: Niyazi |
last post by:
Hi everyone,
I have a sql table that has 5 column as:
cl1Month - cl1_3Month - cl3_6Month - cl6_12Month - clMoreThan12Month
Now I have to date and I have to find the differences and check as...
|
by: G |
last post by:
Hello,
I have two text fields, DateOfBirth and TodaysDate
DateOfBirth:
16/01/1980
TodaysDate:
04/06/2007
|
by: W. eWatson |
last post by:
Are there some date and time comparison functions that would compare, say,
Is 10/05/05 later than 09/22/02? (or 02/09/22 format, yy/mm/dd)
Is 02/11/07 the same as 02/11/07?
Is 14:05:18 after...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |