944,007 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jul 15th, 2008
0

How to compare dates through javascripts

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Jul 15th, 2008
1

Re: How to compare dates through javascripts

javascript Syntax (Toggle Plain Text)
  1. if (Date.parse(fromDate) > Date.parse(toDate)) {
  2. alert("Invalid Date Range!\nStart Date cannot be after End Date!")
  3. return false;
}

or

php Syntax (Toggle Plain Text)
  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
javascript Syntax (Toggle Plain Text)
  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 C; Jul 15th, 2008 at 8:36 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Jul 15th, 2008
0

Re: How to compare dates through javascripts

always mark thread as solved, when you get solution, as it will help others if they are searching for a solution.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Jul 15th, 2008
0

Re: How to compare dates through javascripts

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, 23 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Jul 15th, 2008
0

Re: How to compare dates through javascripts

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...
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Jul 16th, 2008
0

Re: How to compare dates through javascripts

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Jul 16th, 2008
0

Re: How to compare dates through javascripts

Click to Expand / Collapse  Quote originally posted by gargg321 ...
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:
php Syntax (Toggle Plain Text)
  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....
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Jul 16th, 2008
0

Re: How to compare dates through javascripts

How could I pass this value to the javascripts for validation.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Jul 16th, 2008
0

Re: How to compare dates through javascripts

Click to Expand / Collapse  Quote originally posted by gargg321 ...
How could I pass this value to the javascripts for validation.
Use this format..
javascript Syntax (Toggle Plain Text)
  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 C; Jul 16th, 2008 at 3:41 am. Reason: add more information
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Jul 16th, 2008
0

Re: How to compare dates through javascripts

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: unable to swap images using arrays in javascript
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: how to display only checked value in checkbox by using javascript?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC