Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to compare two date

Status
Not open for further replies.

jimmyweb

Programmer
Dec 9, 2004
37
0
0
US
Hi friends,
I created a simple javascript to validate start and end date.
if start date less than end date, system will display an error message.
But it does not wotking. however, alert function display correct date.
Thanks for any help!
Jim
******** codes
<script language="JavaScript">
function checkEntry() {
alert(document.optSel.p_startdt.value );
alert(document.optSel.p_enddt.value);
if (document.optSel.p_startdt.value > document.optSel.p_enddt.value
{
alert("Please enter start date less than end date");
document.optSel.p_startdt.focus();
return false;
}
else
{
return true;
}
}
</script>
 
You'd want to firstly put the values from the fields into JavaScript [tt]Date()[/tt] objects.

Code:
//Put the start date field into a date object
var strStartDate = document.optSel.p_startdt.value;
var dtStartDate = new Date(Date.parse(strStartDate));

And, of course, do the same for the end date.

Then, as JavaScript stores date values as an integer number of milliseconds, you can subtract one from the other and look at the result.

Code:
//compare two dates
if(dtStartDate <= dtFinishDate){
 alert("Congratulations, you pass logic 101.");
}
else{
 alert("Hmmm... not so sure about that.");
}

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Hi dwarfthrower,
Thanks for your email. it works. But why do we need to use Date.parse....

Jim
 
HiFriends and dwarfthrower,
Do we have any wey to highlight that input text box?
selected is only for checkbox?
any ideal?
Thanks for any help?
Jim
 
Jim,
>> "Do we have any wey to highlight that input text box?"
use the select() method on an input to select the contents of that input.

e.g.
Code:
var box = document.getElementById("myFormId").myInputField;
box.select();

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top