473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Date calculation not quite functioning correct.

57
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???


Expand|Select|Wrap|Line Numbers
  1. <script>
  2.     function checkadate(){
  3.  
  4. //This bit gets todays date and add seven days
  5. var currentdate = new Date();
  6. currentdate.setDate(currentdate.getDate()+7);
  7. var day = currentdate.getDate();
  8. var month = currentdate.getMonth() + 1;
  9. var year = currentdate.getFullYear();
  10.  
  11. //This bit puts that date in to dd/mm/yyyy format
  12. var todat = (day + "/" + month + "/" + year)
  13.  
  14. alert("Todays date plus 7 is " +todat);
  15.  
  16. //This is the date entered by the user.
  17. var ReqDate =  document.getElementById("DateRequired").value;
  18.  
  19. alert("Selected date is " + ReqDate);
  20.  
  21. var SDate = (todat);
  22. var EDate =  document.getElementById("DateRequired").value;
  23. var endDate = new Date(EDate);
  24. var startDate= new Date(SDate);
  25.  
  26.  
  27. if(startDate > endDate){
  28.     alert("Date must be at least 5 working days in advance to allow for processing.\nThe earliest date available is " + SDate);
  29.     document.getElementById("DateRequired").value = SDate;
  30.     return false;
  31.     }else{
  32. }
  33.  
  34.  
  35.  
  36. }
  37. </script>
Apr 25 '08 #1
7 1629
acoder
16,027 Expert Mod 8TB
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.
Apr 25 '08 #2
plumba
57
Pardon my ignorance, but I'm not quite following....... :/
Apr 28 '08 #3
acoder
16,027 Expert Mod 8TB
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.
Apr 28 '08 #4
plumba
57
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...

Expand|Select|Wrap|Line Numbers
  1. <script>
  2.     function checkadate(){
  3.  
  4. //Gets todays date and add seven days.
  5. var currentdate = new Date();
  6. currentdate.setDate(currentdate.getDate()+7);
  7. alert(currentdate);
  8.  
  9.  
  10. //Gets date entered
  11. var ReqDate =  document.getElementById("DateRequired").value;
  12.  
  13. //Splits date into an array
  14. var ReqArray=ReqDate.split("/");
  15. alert(ReqArray);
  16.  
  17. var ReqDate = new Date();
  18. ReqDate.setFullYear(ReqArray);
  19. alert(ReqDate);
  20. //**Failing here**
  21.  
  22.  
  23. if(ReqDate > currentdate){
  24.     alert("Date must be at least 5 working days in advance to allow for processing.\nThe earliest date available is " + SDate);
  25.     document.getElementById("DateRequired").value = SDate;
  26.     return false;}
  27.  
  28. }
  29. </script>
Apr 29 '08 #5
acoder
16,027 Expert Mod 8TB
Use the array values separately and see how setFullYear is also used (with three parameters).
Expand|Select|Wrap|Line Numbers
  1. //Gets date entered
  2. var ReqDate =  document.getElementById("DateRequired").value;
  3.  
  4. //Splits date into an array
  5. var ReqArray=ReqDate.split("/");
  6.  
  7. var ReqDate = new Date();
  8. ReqDate.setFullYear(ReqArray[2],ReqArray[1]-1,ReqArray[0]);
  9.  
Apr 29 '08 #6
plumba
57
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.

<
Expand|Select|Wrap|Line Numbers
  1. script>
  2. function comparetwodates(){
  3. //Gets todays date and add seven days.
  4. var currentdate = new Date();
  5. currentdate.setDate(currentdate.getDate()+7);
  6.  
  7. //Gets date entered
  8. var myDate =  document.getElementById("mydate").value;
  9.  
  10. //Splits date into three components
  11. var ReqArray=myDate.split("/"); //Or change the '/' for your date seperator
  12.  
  13. //Pieces date back as a date
  14. var myDate = new Date();
  15. ReqDate.setFullYear(ReqArray[2],ReqArray[1]-1,ReqArray[0]);
  16.  
  17.  
  18. //Checks to see if myDate is earlier than currentdate
  19. if(ReqDate < currentdate){
  20.  
  21.     alert("My Date is less than 7 days from today.");
  22.                 return false;
  23. }
  24. }
  25. </script>
Apr 30 '08 #7
acoder
16,027 Expert Mod 8TB
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.
Apr 30 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

20
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...
1
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...
5
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...
1
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...
2
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....
2
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...
2
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...
3
by: G | last post by:
Hello, I have two text fields, DateOfBirth and TodaysDate DateOfBirth: 16/01/1980 TodaysDate: 04/06/2007
16
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...
0
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
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.