js 计算两个日期的差值,刨除当前日期:
function timeDifc(start, end) { var starts = new Date(start), ends = new Date(end), message = ''; if (starts.getTime() > ends.getTime()) return message = "现在的时间小于以前的时间!"; //if ((ends.getTime() - starts.getTime()) / (1000 * 60) < 1) return message = "刚刚"; // if (ends.getFullYear() > starts.getFullYear() && ends.getMonth() >= starts.getMonth()) message += ends.getFullYear() - starts.getFullYear() + "年"; if (ends.getFullYear() > starts.getFullYear()) { if (ends.getDate() >= starts.getDate()-1) { message += ends.getMonth() + 12 - starts.getMonth() + "月"; message += ends.getDate() - starts.getDate()-1 + "天"; } else { message += ends.getMonth() + 11 - starts.getMonth() + "月"; switch (ends.getMonth()) { case 0: case 2: case 4: case 6: case 7: case 9: case 11: message += ends.getDate() + 31 - starts.getDate()-1 + "天"; break; case 3: case 5: case 8: case 10: message += ends.getDate() + 30 - starts.getDate()-1 + "天"; break; case 1: if (ends.getFullYear() % 4 === 0) { message += ends.getDate() + 29 - starts.getDate()-1 + "天"; break; } else { message += ends.getDate() + 28 - starts.getDate()-1 + "天"; } } } } else { if (ends.getMonth() > starts.getMonth() && ends.getDate()-1 >= starts.getDate()) { message += ends.getMonth() - starts.getMonth() + "月"; message += ends.getDate() - starts.getDate() + "天"; } if (ends.getMonth() > starts.getMonth() && ends.getDate()-1 < starts.getDate()) { message += ends.getMonth() - 1 - starts.getMonth() + "月"; switch (ends.getMonth()) { case 0: case 2: case 4: case 6: case 7: case 9: case 11: message += ends.getDate() + 31 - starts.getDate()-1 + "天"; break; case 3: case 5: case 8: case 10: message += ends.getDate() + 30 - starts.getDate()-1 + "天"; break; case 1: if (ends.getFullYear() % 4 === 0) { message += ends.getDate() + 29 - starts.getDate()-1 + "天"; break; } else { message += ends.getDate() + 28 - starts.getDate()-1 + "天"; } } } } //if (ends.getHours() > starts.getHours() && ends.getMinutes() >= starts.getMinutes()) message += ends.getHours() - starts.getHours() + "小时"; //if (ends.getMinutes() > starts.getMinutes()) message += ends.getMinutes() - starts.getMinutes() + "分钟"; return message; }
讯享网
调用示例:
var diffDay = timeDifc("2019-03-26", "2019-07-02");
获得的结果是:"3月6天"
2019-07-10:
第二种方式
讯享网DateDifference = function (begindate, enddate) { // 拆分年月日 dateArry1 = begindate.split('-'); // 得到月数 date1 = parseInt(dateArry1[0]) * 12 + parseInt(dateArry1[1]); // 拆分年月日 dateArry2 = enddate.split('-'); // 得到月数 date2 = parseInt(dateArry2[0]) * 12 + parseInt(dateArry2[1]); var m = Math.abs(date1 - date2); if (m === 0) { //当月 return "0" + "^" + Math.abs(parseInt(dateArry2[2]) - parseInt(dateArry1[2])); } else { if (parseInt(dateArry1[2]) < parseInt(dateArry2[2])) { return m + "^" + Math.abs(parseInt(dateArry2[2]) - parseInt(dateArry1[2])); } else { m = m - 1; var month = parseInt(dateArry2[1]) - 1; if (month === 0) { month = 12; } var montharry = [1, 3, 5, 7, 8, 10, 12]; if (montharry.indexOf(month) !==-1) { return m + "^" + Math.abs(parseInt(dateArry2[2]) + 31 - parseInt(dateArry1[2])); } else { return m + "^" + Math.abs(parseInt(dateArry2[2]) + 30 - parseInt(dateArry1[2])); } } } }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/100048.html