简体   繁体   English

如何比较两个日期并通过javascript将其放入文本框

[英]How to compare the two dates and result it into the textbox by javascript

My web page includes a table with multiple columns, In that there are two columns depends on the dates which is the user input. 我的网页包含一个包含多列的表格,其中有两列取决于用户输入的日期。 My need here is compare the two dates of the textboxes and result will come in the another textbox. 我在这里需要比较文本框的两个日期,结果将出现在另一个文本框中。 for example 例如 在此处输入图片说明

In this I need to compare the current date and actual date. 在此,我需要比较当前日期和实际日期。 If the date exceed more than 7 days from the actual then status column will display the red color, if 4 days then green color else it is equal then yellow color. 如果日期距实际日期超过7天,则状态栏将显示红色,如果4天则显示绿色,否则等于黄色。 I don't know which function to use. 我不知道要使用哪个功能。

My code for the table: 我的表代码:

<table id="POITable">
  <tr>
    <th width="100px" style="display:none">SL.no</th>
    <th width="100px">col1</th>
    <th width="85px">col2</th>
    <th width="85px">col3</th>
    <th width="85px">col4</th>
    <th width="95px">col5</th>
    <th width="100px">Delete/<input type="button" id="addmorePOIbutton" value="Add" onclick="insRow()" /></th>
  </tr>
  <tr>
    <td style="display:none">1</td>
    <td>
      <input type="text" id="txtAutoComplete" list="languageList" style="border:none;font-size:10pt;width:100px;" />
      <!--your input textbox-->
      <datalist id="languageList">
    <option value="Dddd" />
    <option value="DTdsds" />
    <option value="adsda" />
    <option value="adsadsad" />
    <option value="dadsada" />
    <option value="rsfsfsdfs" />
    <option value="Csffsf" />
    </datalist>
    </td>
    <td><input type="text" id="txtbox" name="name" style="border:none;font-size:10pt;width:80px;"></td>
    <td><input type="text" id="txtbox" name="name" style="border:none;font-size:10pt;width:75px;"></td>
    <td><input type="text" id="txtbox" name="name" style="border:none;font-size:10pt;width:75px;"></td>
    <td><input type="text" id="txtbox" name="name" style="border:none;font-size:10pt;width:75px;"></td>
    <td><input type="button" id="delPOIbutton" value="Delete" onclick="deleteRow(this)" /></td>
  </tr>

  </tr>
</table>

To build on what DelightedD0D said, the moment.js library makes this calculation very easy. 为了建立DelightedD0D所说的内容,moment.js库使此计算非常容易。 First, add these classes to your css file: 首先,将这些类添加到您的css文件中:

.greenBg {
    background: green;
}

.yellowBg {
    background: yellow;
}

.redBg {
    background: red;
}

Javascript: Javascript:

$(function() {
    //get current time
    var currentTime = moment();

    //example parsing format moment("12-25-1995", "MM-DD-YYYY")
    var actualTime = moment(/*actual time in string, format*/);

    var diff = currentTime.diff(actualTime, 'days');

    if(diff > 7)
    {
         $("#/*statusboxid*/").addClass('redBg');
    }
    else if(diff > 4)
    {
        $("#/*statusboxid*/").addClass('greenBg');
    }
    else
    {
        $("#/*statusboxid*/").addClass('yellowBg');
    }
});

It is not 100% clear what your formatting logic is you can adjust the if-else structure as needed. 尚不清楚您的格式化逻辑是什么,您是否可以根据需要调整if-else结构。 Also, it's not clear from code what the id is for the table cells that contain the current, actual time, or status so placeholders have been used. 另外,从代码中还不清楚包含当前,实际时间或状态的表单元格的ID是什么,因此已使用占位符。

This code is works for me. 该代码对我有效。

function compare1() {

  var firstDate = moment($("#date1").val(), "MM-DD-YYYY");
  var secondDate = moment($("#date2").val(), "MM-DD-YYYY");

  console.log(firstDate.inspect(), secondDate.inspect());

  if (firstDate.isValid() && secondDate.isValid()) {
    // you need a validation before using diff function of momentjs
    var diff = Math.abs(firstDate.diff(secondDate, 'days'));
    console.log(diff);
    // you also need to remove all classes before adding new class
    $("#status1").removeClass("redBg").removeClass("greenBg").removeClass("yellowBg");
    if (diff >= 14) {
      $("#status1").addClass('redBg');
    } else if (diff >= 8 && diff <= 13 ) {
      $("#status1").addClass('yellowBg');
    } else if (diff >= 1 && diff <=7){
      $("#status1").addClass('greenBg');
    }
  } else {
    $("#status1").addClass('greenBgBg');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM