How to compare dates through javascripts

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

How to compare dates through javascripts

 
0
  #1
Jul 15th, 2008
Hi,

I am developing a module in PHP where I have to ask user to enter dates in two fields. The catch is that the second date entered should come after the first one in a calendar year. eg- if first date field has value of June 2,2008 then the second field should have the date after June 2,2008. It can't have any date before june 2,2008. To validate this I have to use javascripts but it is the first time I am using javascripts. So please refer me afunction that could compare the two fields and act accordingly
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: How to compare dates through javascripts

 
1
  #2
Jul 15th, 2008
  1. if (Date.parse(fromDate) > Date.parse(toDate)) {
  2. alert("Invalid Date Range!\nStart Date cannot be after End Date!")
  3. return false;
}

or

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <meta name="Content-Script-Type" content="text/javascript">
  7. <meta name="Content-Style-Type" content="text/css">
  8. <title>Example</title>
  9.  
  10. <script type="text/javascript">
  11. <!--
  12. function check (f) {
  13. var start = new Date (f.start.value);
  14. var end = new Date (f.end.value);
  15.  
  16. start.setDate (start.getDate() + 28);
  17. if (end >= start) {alert (); return false}
  18. }
  19. // -->
  20. </script>
  21.  
  22. <style type="text/css">
  23. <!--
  24. fieldset {padding:1ex; width:10em}
  25. label {display:block; margin:1em 0}
  26. input {display:block}
  27. button {display:block; margin:auto}
  28. -->
  29. </style>
  30.  
  31. </head>
  32. <body>
  33. <form action="some-script.pl" onsubmit="return check (this)">
  34. <fieldset>
  35. <legend>Dates</legend>
  36. <label>Start Date<input name="start" onchange="this.value = new Date (this.value).toDateString()" type="text"></label>
  37. <label>End Date<input name="end" onchange="this.value = new Date (this.value).toDateString()" type="text"></label>
  38. <button type="submit">Submit</button>
  39. </fieldset>
  40. </form>
  41. </body>
  42. </html>


or simply
  1. function dt()
  2. {
  3. var ch=Date.parse("06/19/2008");
  4.  
  5. var currentdt=new Date;
  6. if(ch>Date.parse(currentdt))
  7. {
  8. alert("Date is greater.");
  9. }
  10. else
  11. {
  12. alert("Date is less");
  13. }
  14. }
Last edited by Shanti Chepuru; Jul 15th, 2008 at 8:36 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: How to compare dates through javascripts

 
0
  #3
Jul 15th, 2008
always mark thread as solved, when you get solution, as it will help others if they are searching for a solution.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

Re: How to compare dates through javascripts

 
0
  #4
Jul 15th, 2008
Shanti I would have marked the thread as solved if my problem would have solved but it isn't. I don't know why but the function you mentioned isn't helping me. I am attaching the file in which the validation has to be made. I have made changes in it at line number 166. Please have a look.
Attached Files
File Type: php campaign_info_tableadd.php (139.4 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: How to compare dates through javascripts

 
0
  #5
Jul 15th, 2008
Hello..
i have posted another two ways which i have posted...
try to implement with those also...
then tel me where will be the error...
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

Re: How to compare dates through javascripts

 
0
  #6
Jul 16th, 2008
I have tried every solution you have given me, Shanti. It's not working. The program isn't generating any error. I think this has got something to do with the format, the javascript is getting the dates. The format it would be recieving dates would be of sort yyyy/mm/dd. That is why it can't compare the dates. Please tell me some solution so that I could compare them in this format.
Last edited by gargg321; Jul 16th, 2008 at 2:00 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: How to compare dates through javascripts

 
0
  #7
Jul 16th, 2008
Originally Posted by gargg321 View Post
I have tried every solution you have given me, Shanti. It's not working. The program isn't generating any error. I think this has got something to do with the format, the javascript is getting the dates. The format it would be recieving dates would be of sort yyyy/mm/dd. That is why it can't compare the dates. Please tell me some solution so that I could compare them in this format.
Helloo,
By this code convert your date to which format you want:
  1. $dp=$_POST['date'];
  2. $dp1=explode("/",$dp);
  3. $dp2=$dp1[2]."-".$dp1[0]."-".$dp1[1];
In the last line change format according to your application....
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

Re: How to compare dates through javascripts

 
0
  #8
Jul 16th, 2008
How could I pass this value to the javascripts for validation.
Commitment is what transforms a promise into reality. It is the words that speak boldly of your intentions. And the actions which speak louder than the words. It is making the time when there is none. Commitment is the stuff character is made of; the power to change the face of things. It is the daily triumph of integrity over skepticism.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,076
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: How to compare dates through javascripts

 
0
  #9
Jul 16th, 2008
Originally Posted by gargg321 View Post
How could I pass this value to the javascripts for validation.
Use this format..
  1.  
  2. StartDate.setFullYear(StartDateSt);
  3. StartDate.setFullYear(StartDateSt[0],StartDateSt[1],StartDateSt[2]);


The format which i have posted earily is for php...sorry...


Check this manual for reference:
http://www.w3schools.com/js/js_obj_date.asp
Last edited by Shanti Chepuru; Jul 16th, 2008 at 3:41 am. Reason: add more information
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

Re: How to compare dates through javascripts

 
0
  #10
Jul 16th, 2008
Thanx Shanti. The job has been done. There was a error from my side that I was overlooking. Your first solution does the work for me. Thanx again
Commitment is what transforms a promise into reality. It is the words that speak boldly of your intentions. And the actions which speak louder than the words. It is making the time when there is none. Commitment is the stuff character is made of; the power to change the face of things. It is the daily triumph of integrity over skepticism.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC