這是我的時間差來計算日子的代碼和我的yy-dd-mm
用戶的日期格式,但我已經改變了我的格式dd-mm-yy
。所以我已經改變了格式代碼,但它不能正常工作。 請任何人都可以在這方面幫助?更改日期甲
<script>
var calculate = function() {
var from = document.getElementById("txtdate").value;
var fromdate = from.slice(3, 5);
fromdate = parseInt(fromdate);
var frommonth = from.slice(0, 2);
frommonth = parseInt(frommonth);
var fromyear = from.slice(6, 10);
fromyear = parseInt(fromyear);
var to = document.getElementById("txtdate1").value;
var todate = to.slice(3, 5);
todate = parseInt(todate);
var tomonth = to.slice(0, 2);
tomonth = parseInt(tomonth);
var toyear = to.slice(6, 10);
toyear = parseInt(toyear);
var oneDay = 24*60*60*1000;
var firstDate = new Date(fromyear,frommonth,fromdate);
var secondDate = new Date(toyear,tomonth,todate);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))+1);
if (diffDays)
document.getElementById("result").value=diffDays;
}
</script>
Checkout moment.js庫,它真的很棒,並會幫助你添加/減少/創建差異:) – PolishDeveloper