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

How to Format time that is input by user.

Status
Not open for further replies.

bradly999

Technical User
Oct 9, 2001
2
CA
I have a text field that populated by the user for start time and another that populated by the user with an end time. I will need to format the time and calculate the difference. Start and end times could be from a specific day as old as 60 days ago so now() doesnt help at all.

Thanks
999
 
Here's the code we use for setting up and comparing dates in JavaScript. On my site, my visitors select the month from a drop down list, the day from a drop down list, the year from a drop down list, the hours from a drop down list, minutes from drop down list and the am /pm from a third drop down list. There are two sets of these on this page, (start date/time and end date/time). This script compares the two and prevents the end date from being earlier than the start date. It's probably not exactly what you're looking for, but I'm sure you can learn something from it.

var syearvar = parseFloat(form_obj.syear.options[form_obj.syear.selectedIndex].value)
var smonthvar = parseFloat(form_obj.smonth.options[form_obj.smonth.selectedIndex].value)
var sdayvar = parseFloat(form_obj.sday.options[form_obj.sday.selectedIndex].value)
var shourvar = parseFloat(form_obj.shour.options[form_obj.shour.selectedIndex].value)
var sminvar = parseFloat(form_obj.smin.options[form_obj.smin.selectedIndex].value)
if (form_obj.sampm.options[form_obj.sampm.selectedIndex].value == "pm") {
if (shourvar < 12) {
shourvar = shourvar + 12
}
} else {
if (shourvar == 12) {
shourvar = 0
}
}

var eyearvar = parseFloat(form_obj.eyear.options[form_obj.eyear.selectedIndex].value)
var emonthvar = parseFloat(form_obj.emonth.options[form_obj.emonth.selectedIndex].value)
var edayvar = parseFloat(form_obj.eday.options[form_obj.eday.selectedIndex].value)
var ehourvar = parseFloat(form_obj.ehour.options[form_obj.ehour.selectedIndex].value)
var eminvar = parseFloat(form_obj.emin.options[form_obj.emin.selectedIndex].value)
if (form_obj.eampm.options[form_obj.eampm.selectedIndex].value == &quot;pm&quot;) {
if (ehourvar < 12) {
ehourvar = ehourvar + 12
}
} else {
if (ehourvar == 12) {
ehourvar = 0
}
}

var begdate = new Date(syearvar,smonthvar-1,sdayvar,shourvar,sminvar,00)
var enddate = new Date(eyearvar,emonthvar-1,edayvar,ehourvar,eminvar,00)

if (enddate < begdate) {
window.alert(&quot;The Delivery End Date / Time is less than the Beginning End Date / Time. Please correct before continuing.&quot;)
form_obj.emonth.focus()
return false
}

This covers the date object and the comparison.

TW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top