| | |
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:
Solved Threads: 0
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
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
javascript Syntax (Toggle Plain Text)
if (Date.parse(fromDate) > Date.parse(toDate)) { alert("Invalid Date Range!\nStart Date cannot be after End Date!") return false;
or
php Syntax (Toggle Plain Text)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="Content-Script-Type" content="text/javascript"> <meta name="Content-Style-Type" content="text/css"> <title>Example</title> <script type="text/javascript"> <!-- function check (f) { var start = new Date (f.start.value); var end = new Date (f.end.value); start.setDate (start.getDate() + 28); if (end >= start) {alert (); return false} } // --> </script> <style type="text/css"> <!-- fieldset {padding:1ex; width:10em} label {display:block; margin:1em 0} input {display:block} button {display:block; margin:auto} --> </style> </head> <body> <form action="some-script.pl" onsubmit="return check (this)"> <fieldset> <legend>Dates</legend> <label>Start Date<input name="start" onchange="this.value = new Date (this.value).toDateString()" type="text"></label> <label>End Date<input name="end" onchange="this.value = new Date (this.value).toDateString()" type="text"></label> <button type="submit">Submit</button> </fieldset> </form> </body> </html>
or simply
javascript Syntax (Toggle Plain Text)
function dt() { var ch=Date.parse("06/19/2008"); var currentdt=new Date; if(ch>Date.parse(currentdt)) { alert("Date is greater."); } else { alert("Date is less"); } }
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..
•
•
Join Date: Jun 2008
Posts: 21
Reputation:
Solved Threads: 0
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.
•
•
•
•
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.
By this code convert your date to which format you want:
php Syntax (Toggle Plain Text)
$dp=$_POST['date']; $dp1=explode("/",$dp); $dp2=$dp1[2]."-".$dp1[0]."-".$dp1[1];
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
•
•
Join Date: Jun 2008
Posts: 21
Reputation:
Solved Threads: 0
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.
•
•
•
•
How could I pass this value to the javascripts for validation.
javascript Syntax (Toggle Plain Text)
StartDate.setFullYear(StartDateSt); 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..
•
•
Join Date: Jun 2008
Posts: 21
Reputation:
Solved Threads: 0
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.
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: unable to swap images using arrays in javascript
- Next Thread: how to display only checked value in checkbox by using javascript?
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate array automatically beta box bug calendar cart checkbox child class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element enter error explorer file firefox focus forms frameworks getselection google gwt html htmlform ie8 iframe images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent php player post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle variables w3c web website window windowofwords windowsxp wysiwyg \n






