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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date difference

Status
Not open for further replies.

mousematt

MIS
Feb 5, 2001
102
GB
Hi,

I need some help with date calculations. I have a form with a date input box, the date is in for format dd/mm/yyyy the form calls a script before submiting it;

<form action=&quot;new2.asp&quot; method=&quot;post&quot; name=&quot;NewCall&quot; onSubmit=&quot;return submitIt(this)&quot;>

The script looks a bit like;

<script language=&quot;JavaScript&quot;>
function submitIt(NewForm){
if (!(NewForm.NoCallBack.checked)){
if (NewForm.Date.value == &quot;&quot;){
alert('You must enter a date')
return false
}
}
return true
}

I need this to also check if the date is in the past and for the life of me I can't figure it out. Does anyone have a solution.

Matt
 
<script language=&quot;JavaScript&quot;>
function submitIt(NewForm){
if (!(NewForm.NoCallBack.checked)){
if (NewForm.Date.value == &quot;&quot;){
alert('You must enter a date')
return false
}
today = new Date()
theDate = new Date(NewForm.Date.value)
if (theDate == &quot;NaN&quot;){
alert('You must enter a date')
return false
}
if (theDate > today){
alert(&quot;Please enter a date in the past&quot;)
return false
}

}
return true
}
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Sort of works!
I might have confused you, as I wanted to disallow dates in the past and only allow dates in the future. I changed the > to a <. Some dates it works on and some it doesn't, anything on any below 11 it works fine but if the date is 11 or above on any month it doesn't. Any idea?
 
Not sure what's going wrong there. This code works fine...


<script language=&quot;JavaScript&quot;>
function checkDate(){
today = new Date()
theDate = new Date(document.dateForm.dateField.value)
if (isNaN(theDate)){
document.dateForm.result.value = &quot;bad date&quot;
}
else if (theDate > today){
document.dateForm.result.value = &quot;Future&quot;
}
else{
document.dateForm.result.value = &quot;Past&quot;
}

}
</script>

<form name=dateForm>
<input name=dateField>
<input name=result>
<input type=button onClick=&quot;checkDate()&quot; value=&quot;Check Date&quot;>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Ok its not but this time I know why. I'm in the uk, dd/mm/yyyy is doing the calculations in mm/dd/yyyy even though all settings on server are uk!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top