• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Compare two form fields containing begin time and end time in AM PM format in a form

I have a simple question. I have a form called form2 that contains two fields. the Begin Time with id "s1Time1" and an End Time with id of "s1Time2". The two fields hold the formats of HH:MM AM or PM. All I need is to be able to tell in a javascript function that the Begin Time can not be greater than the End Time. I am not using any dates or seconds in my time fields.

This is what my form looks like:

<form id="form1" name="form1" method="post" action="">
  <div id="sample1" class="ui-widget-content" style="padding: .5em;">
      <input name="s1Time1" id="s1Time1" value=""/>
      <input name="s1Time2" id="s1Time2" value="" />
  <input name="submitbtn" type="submit" id="submitbtn"  value="Submit" />
1 Solution
var stime = document.getElementById("s1Time1").value;
var etime = document.getElementById("s1Time2").value;

var stime = new Date("January 01, 2000 " + stime);
stime = stime.getTime();

var etime = new Date("January 01, 2000 " + etime);
etime = etime.getTime();

if (stime > etime) {
} else {
    alert("valid time")

Open in new window

Didn't do too much testing but it should be fine.

You can test here
kbsad1Author Commented:
Worked great . Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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