0

In my form I separated the date picker and time picker. Now I want to create a date with hours.

<div class="form-group col-lg-8 col-lg-offset-2">
  <div class="col-lg-6" style="padding-left: 0;">
      <label class="control-label " for="startDate">Start Date</label>
      <input type="text" class=" required form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose" name="startDate">
  </div>
  <div class="col-lg-6" style="padding: 0;">
      <label class="control-label " for="startTime">Start Time</label>
      <div class="bootstrap-timepicker">
          <input id="timepicker3" name="startTime" type="text" class="required form-control">
      </div>
  </div>
  </div>

  <div class="form-group col-lg-8 col-lg-offset-2">
  <div class="col-lg-6" style="padding-left: 0;">
      <label class="control-label " for="endDate">End Date</label>
      <input type="text" class=" required form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose2" name="endDate">
  </div>
  <div class="col-lg-6" style="padding: 0;">
      <label class="control-label " for="endTime">End Time</label>
      <div class="bootstrap-timepicker">
          <input id="timepicker4" name="endTime" type="text" class="required form-control">
      </div>
  </div>
  </div>

  <div class="form-group col-lg-8 col-lg-offset-2">
  <div class="col-lg-6" style="padding-left: 0;">
      <label class="control-label " for="deadlineDate">Registration Deadline Date</label>
      <input type="text" class=" required form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose5" name="deadlineDate">
  </div>
  <div class="col-lg-6" style="padding: 0;">
      <label class="control-label " for="deadlineTime">Registration Deadline Time</label>
      <div class="bootstrap-timepicker">
          <input id="timepicker5" name="deadlineTime" type="text" class="required form-control">
      </div>
  </div>
  </div>

Just your own examples will help me. My end goal is to validate the dates which goes as Start Date - should be greater than current time End Date - should be greater than Start Date Registration Deadline - should be greater than current time but less than start date

franz joseph
  • 174
  • 2
  • 13
  • Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) after [What is the best way to parse a time into a Date object from user input in Javascript?](//stackoverflow.com/q/141348) and [How can I convert string to datetime with format specification in JavaScript?](//stackoverflow.com/q/476105) – Heretic Monkey Mar 06 '18 at 20:41

2 Answers2

0

IIUC

datestr = '2018/03/06'
timestr = '13:10'
function toDate(dateStr, timeStr) { // assuming its a YYYY/MM/DD string
    var dateParts = dateStr.split("/");
    var timeParts = timeStr.split(":");
    date = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
    date.setHours(timeParts[0]);
    date.setMinutes(timeParts[1]);
    return date;
}
IWHKYB
  • 481
  • 3
  • 11
0

When you create a Date, it produces a date/time object that can be examined as an integer (use Date.now() to get this). You can then compare one integer against another (date against date). Whichever integer is greater is the one that is most current.

var d1 = Date.now();
var d2 = null;
// Wait 2 seconds and make another date
setTimeout(function(){ 
  d2 = Date.now(); 

  // Look at what we have
  console.log("d1 is: " + d1.toLocaleString(), " - d2 is: " + d2.toLocaleString());
  console.log("The difference between the dates/times is: " + ((d2 - d1) / 1000) + " seconds.");
  
  // Compare:
  if(d1 > d2) {
    console.log("d1 is more current than d2");
  } else {
    console.log("d2 is more current than d1"); 
  }

}, 2000);
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71