簡體   English   中英

用輸入時間date html的2個日期之間的javascript時間計算

[英]Calculate in javascript time between 2 dates from input type date html

我想計算從html(輸入類型日期值)獲得的2個日期之間的時間范圍。

我想獲取年,月和日的時間表。

即:2017-01-01&2019-02-15

2年1個月14天

謝謝,

你可以使用時刻庫

var a = moment([2017, 1, 1]);
var b = moment([2019, 2,15]);

b.diff(a, 'days')  //get difference in days.. 
b.diff(a, 'months') //get difference in months.
b.diff(a, 'years') //get difference in years.. 

要么

diff = b.diff(a);

years = moment.duration(diff).get(years);
months = moment.duration(diff).get(months);
days = moment.duration(diff).get(days); 

您可以在這里找到該庫。Moment.js庫

我希望這有幫助..

您可以使用此功能。

<script>
    function getDateDifference(startDate, endDate) {
      if (startDate > endDate) {
        console.error('Start date must be before end date');
        return null;
      }
      var startYear = startDate.getFullYear();
      var startMonth = startDate.getMonth();
      var startDay = startDate.getDate();

      var endYear = endDate.getFullYear();
      var endMonth = endDate.getMonth();
      var endDay = endDate.getDate();

      var february = (endYear % 4 == 0 && endYear % 100 != 0) || endYear % 400 == 0 ? 29 : 28;
      var daysOfMonth = [31, february, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

      var startDateNotPassedInEndYear = (endMonth < startMonth) || endMonth == startMonth && endDay < startDay;
      var years = endYear - startYear - (startDateNotPassedInEndYear ? 1 : 0);

      var months = (12 + endMonth - startMonth - (endDay < startDay ? 1 : 0)) % 12;

      var days = startDay <= endDay ? endDay - startDay : daysOfMonth[(12 + endMonth - 1) % 12] - startDay + endDay;

      return {
        years: years,
        months: months,
        days: days
      };
    }

    var result = getDateDifference(new Date("2018-01-01"), new Date("2019-01-02"));

    //Print in console array with value
    console.log(result);

    var text = result.years+" year, "+result.months+" month, "+result.days+" days";
    alert(text);
</script>

使用getDateDifference和參數第一個日期和第二個日期。 在警報中,我插入演示打印。

 function getTimeBetweenDates(e) { var date1 = new Date(document.getElementById("date1").value); var date2 = new Date(document.getElementById("date2").value); var diff; if (date1 < date2) { diff = new Date(date2 - date1); } else { diff = new Date(date1 - date2); } var years = (diff.getFullYear() - 1970); var months = diff.getMonth(); var days = diff.getDate(); var yearTxt = "year"; var monthTxt = "month"; var dayTxt = "day"; if (years > 1) yearTxt += "s"; if (months > 1) monthTxt += "s"; if (days > 1) dayTxt += "s"; if (years >= 0) { document.getElementById("showdiff").innerHTML = years + " " + yearTxt + ", " + months + " " + monthTxt + ", " + days + " " + dayTxt; } else { document.getElementById("showdiff").innerHTML = "Equal dates"; } } 
 <form id="form1" action="" method="" onSubmit="event.preventDefault(); getTimeBetweenDates();"> <input type="date" id="date1"> <input type="date" id="date2"> </form> <br> <button type="submit" form="form1" value="Send">Send</button> <br> <p id="showdiff"></p> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM