[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Time Comparison

Hi,
I'm trying to write javascript which checks that arrival time is less than departure time. I tried to google it but i guess i'm doing something wrong. Time format would be 01:30PM/AM/am/pm.

.cfm file...
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Archive Form</title>
</head>
<script type="text/javascript" src="sample.js"></script>
<script type="text/javascript" language="javascript">
    function chkExistingRecord() {
		if(!compareTime())
		return false;
    }
</script>
<body>
<cfform onsubmit="return chkExistingRecord(_CF_this)">
PATIENTS REF:<cfinput type="text" name="PatientsRef" id="PatientsRef" value="65" required="true" validate="integer"><br />
VISIT DATE:<cfinput type="text" name="VisitDate" id="VisitDate" value="05/05/2010" required="true" validate="date"><br />
PATIENTS NAME:<cfinput type="text" name="patientName" id="patientName" value="Helloworld"><br />
ARRIVAL TIME:<cfinput type="text" name="ArrivalTime" id="ArrivalTime" value="08:30AM" required="true"><br />
DEPARTURE TIME:<cfinput type="text" name="DepartureTime" id="DepartureTime" value="08:04AM" required="true"><br />
<cfinput type="submit" name="submit" value="SUBMIT FORM" id="submit">
</cfform>
</body>
</html>

Open in new window


javascript file..
function compareTime(){
		 
var start = document.getElementById('ArrivalTime').value;
var end = document.getElementById('DepartureTime').value;
var dtStart = new Date("document.getElementById('VisitDate').value " + start);
var dtEnd = new Date("document.getElementById('VisitDate').value " + end);
difference_in_milliseconds = dtEnd - dtStart;
if (difference_in_milliseconds < 0)
{
      alert("departure time MUST BE GREATER THAN arrival time!");
}
}

Open in new window

0
s_hausen
Asked:
s_hausen
  • 4
  • 3
1 Solution
 
sjklein42Commented:
Try

difference_in_milliseconds = dtEnd.getTime() - dtStart.getTime();

Open in new window

0
 
s_hausenAuthor Commented:
no luck..i even tried this

function compareTime(){
		 
var start = document.getElementById('ArrivalTime').value;
var end = document.getElementById('DepartureTime').value;
var dtStart = new Date("1/1/2007 " + start);
var dtEnd = new Date("1/1/2007 " + end);
/*difference_in_milliseconds = dtEnd - dtStart;*/
difference_in_milliseconds = dtEnd.getTime() - dtStart.getTime();
if (difference_in_milliseconds < 0)
{
      alert("departure time MUST BE GREATER THAN arrival time!");
}
}

Open in new window

0
 
sjklein42Commented:
This works for me:

var dtStart = new Date(document.getElementById('VisitDate').value + ' ' + document.getElementById('ArrivalTime').value);
var dtEnd = new Date(document.getElementById('VisitDate').value + ' ' + document.getElementById('DepartureTime').value);
difference_in_milliseconds = dtEnd - dtStart;

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
s_hausenAuthor Commented:
sjklein42,
i just checked it and nothing works, but if i just only put alert('something'); it does work. any clue???

function compareTime(){
      alert("departure time MUST BE GREATER THAN arrival time!");
}
0
 
sjklein42Commented:
This is the code I tested:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Archive Form</title>
</head>
<script type="text/javascript" src="sample.js"></script>
<script type="text/javascript" language="javascript">
    function chkExistingRecord() {
		if(!compareTime())
		return false;
    }
</script>
<body>
<form onsubmit="return chkExistingRecord(this)">
PATIENTS REF:<input type="text" name="PatientsRef" id="PatientsRef" value="65" required="true" validate="integer"><br />
VISIT DATE:<input type="text" name="VisitDate" id="VisitDate" value="05/05/2010" required="true" validate="date"><br />
PATIENTS NAME:<input type="text" name="patientName" id="patientName" value="Helloworld"><br />
ARRIVAL TIME:<input type="text" name="ArrivalTime" id="ArrivalTime" value="08:30AM" required="true"><br />
DEPARTURE TIME:<input type="text" name="DepartureTime" id="DepartureTime" value="08:04AM" required="true"><br />
<input type="submit" name="submit" value="SUBMIT FORM" id="submit">
</form>
</body>
</html>

<script type="text/javascript" language="javascript">

function compareTime(){
		 
var dtStart = new Date(document.getElementById('VisitDate').value + ' ' + document.getElementById('ArrivalTime').value);
var dtEnd = new Date(document.getElementById('VisitDate').value + ' ' + document.getElementById('DepartureTime').value);
difference_in_milliseconds = dtEnd - dtStart;difference_in_milliseconds = dtEnd - dtStart;
if (difference_in_milliseconds < 0)
{
      alert("departure time MUST BE GREATER THAN arrival time!");
}
}
</script>

Open in new window

0
 
s_hausenAuthor Commented:
Nope, the code doesn't do anything...i guess i better go ahead and use the military time instead of 01:30AM/PM. Anyway, I do appreciate all of your efforts and help. Have a nice day..
0
 
s_hausenAuthor Commented:
did not give the result i was looking for.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now