如何创建日期和日期,然后将其与另一个日期进行比较

时间:2018-03-06 20:25:47

标签: javascript jquery

在我的表格中,我将日期选择器和时间选择器分开。现在我想用小时创建一个日期。



<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>
&#13;
&#13;
&#13;

只是你自己的例子会帮助我。我的最终目标是验证日期 开始日期 - 应大于当前时间 结束日期 - 应大于开始日期 注册截止日期 - 应大于当前时间但小于开始日期

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

当您创建Date时,它会生成一个日期/时间对象,可以将其作为整数检查(使用 Date.now() 来获取此值)。然后,您可以比较一个整数与另一个整数(日期与日期)。无论哪个整数更大都是最新的整数。

&#13;
&#13;
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);
&#13;
&#13;
&#13;