<i id='yq4dd'><tr id='yq4dd'><dt id='yq4dd'><q id='yq4dd'><span id='yq4dd'><b id='yq4dd'><form id='yq4dd'><ins id='yq4dd'></ins><ul id='yq4dd'></ul><sub id='yq4dd'></sub></form><legend id='yq4dd'></legend><bdo id='yq4dd'><pre id='yq4dd'><center id='yq4dd'></center></pre></bdo></b><th id='yq4dd'></th></span></q></dt></tr></i><div id='yq4dd'><tfoot id='yq4dd'></tfoot><dl id='yq4dd'><fieldset id='yq4dd'></fieldset></dl></div>

        <small id='yq4dd'></small><noframes id='yq4dd'>

        <legend id='yq4dd'><style id='yq4dd'><dir id='yq4dd'><q id='yq4dd'></q></dir></style></legend>
        • <bdo id='yq4dd'></bdo><ul id='yq4dd'></ul>

        <tfoot id='yq4dd'></tfoot>
      1. Javascript比较两个日期以获得差异

        时间:2023-08-07
            • <bdo id='EObsu'></bdo><ul id='EObsu'></ul>

            • <legend id='EObsu'><style id='EObsu'><dir id='EObsu'><q id='EObsu'></q></dir></style></legend>

                <tfoot id='EObsu'></tfoot>
                <i id='EObsu'><tr id='EObsu'><dt id='EObsu'><q id='EObsu'><span id='EObsu'><b id='EObsu'><form id='EObsu'><ins id='EObsu'></ins><ul id='EObsu'></ul><sub id='EObsu'></sub></form><legend id='EObsu'></legend><bdo id='EObsu'><pre id='EObsu'><center id='EObsu'></center></pre></bdo></b><th id='EObsu'></th></span></q></dt></tr></i><div id='EObsu'><tfoot id='EObsu'></tfoot><dl id='EObsu'><fieldset id='EObsu'></fieldset></dl></div>

                  <tbody id='EObsu'></tbody>

                <small id='EObsu'></small><noframes id='EObsu'>

                  本文介绍了Javascript比较两个日期以获得差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试比较两个不同的日期,以查看输入的日期是否在今天日期的 7 天之后.我做了一些谷歌搜索并想出了这个:

                  I am trying to compare two different dates to see if the date inputted is after 7 days of todays date. I have done a bit of googling and come up with this:

                  function val_date(input){
                      var date = new Date(input);
                      date = date.getTime() / 1000;
                      var timestamp = new Date().getTime() + (7 * 24 * 60 * 60 * 1000)
                      window.alert("Date: "+date + " = N_Date: "+timestamp);
                      if(timestamp > date || timestamp === date){
                          // The selected time is less than 7 days from now
                          return false;
                      }
                      else if(timestamp < date){
                      // The selected time is more than 7 days from now
                          return true;
                      }
                      else{
                      // -Exact- same timestamps.
                          return false;
                      }
                  }
                  

                  我正在使用提醒,以便我可以检查我的进度以确保日期不同.警报的输出只是说:

                  I am using an alert so that I can check my progress to make sure the dates are different. The output of the alert just says:

                  日期:NaN = N_Date = 13255772630(<- 或类似的东西).

                  Date: NaN = N_Date = 13255772630 (<- or something like that).

                  我在这里做错了吗?
                  不确定是否有帮助,但我的日期格式是 DD-MM-YYYY

                  推荐答案

                  如果你在比较日期并且不想包含时间,你可以使用类似的东西:

                  If you are comparing dates and don't want to include time, you can use something like:

                  // dateString is format DD-MM-YYYY
                  function isMoreThan7DaysHence(dateString) {
                  
                      // Turn string into a date object at 00:00:00
                      var t = dateString.split('-');
                      var d0 = new Date(t[2], --t[1], t[0]);
                  
                      // Create a date for 7 days hence at 00:00:00
                      var d1 = new Date();
                      d1.setHours(0, 0, 0, 0);
                      d1.setDate(d1.getDate() + 7);
                  
                      return d0 >= d1;
                  }
                  

                  请注意,今天日期的小时数必须归零.

                  Note that the hours for today's date must be zeroed.

                  这篇关于Javascript比较两个日期以获得差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么我需要在 JavaScript 中将 unix 时间戳乘以 下一篇:如何按天、周、月对具有时间戳属性的对象进行

                  相关文章

                  最新文章

                  <i id='sXcCj'><tr id='sXcCj'><dt id='sXcCj'><q id='sXcCj'><span id='sXcCj'><b id='sXcCj'><form id='sXcCj'><ins id='sXcCj'></ins><ul id='sXcCj'></ul><sub id='sXcCj'></sub></form><legend id='sXcCj'></legend><bdo id='sXcCj'><pre id='sXcCj'><center id='sXcCj'></center></pre></bdo></b><th id='sXcCj'></th></span></q></dt></tr></i><div id='sXcCj'><tfoot id='sXcCj'></tfoot><dl id='sXcCj'><fieldset id='sXcCj'></fieldset></dl></div>
                  <tfoot id='sXcCj'></tfoot>
                  <legend id='sXcCj'><style id='sXcCj'><dir id='sXcCj'><q id='sXcCj'></q></dir></style></legend>

                      • <bdo id='sXcCj'></bdo><ul id='sXcCj'></ul>

                      <small id='sXcCj'></small><noframes id='sXcCj'>