我尝试过: 函数getDays(strDay1,strDay2){var day = 1000 * 60 * 60 * 24; strDay1 = strDay1 +''+(new Date()。getFullYear()); strDay2 = strDay2 +''+(new Date()。getFullYear()); var d1 = Date.parse(strDay1); var d2 = Date.parse(strDay2); var diff = d2 - d1;返回Math.round(差异/天)} alert(getDays('Nov 8','Mai 5')); for example: Mai 6 (Without 2017) to Nov 7 (Without 2017) DAY:1, DAY:2, DAY:3..... And Nov 8 (Without 2017) to Mai 5 Next year (Without 2018) DAY:1, DAY:2, DAY:3..... I Have thish code but calculating the days only for this year and not from November 8 to next year Mai 5What I have tried:function getDays(strDay1, strDay2){ var day=1000*60*60*24;strDay1 = strDay1 + ' ' + (new Date().getFullYear()); strDay2 = strDay2 + ' ' + (new Date().getFullYear());var d1 = Date.parse(strDay1); var d2 = Date.parse(strDay2);var diff = d2 - d1; return Math.round(diff/day) }alert(getDays('Nov 8', 'Mai 5'));推荐答案 在Javascript中使用以下函数。 函数getDays(strDay1,strDay2){ 调试器; var strMonth1 = strDay1.split('')[0]; var strMonth2 = strDay2.split('')[0]; var months = {JAN:1,FEB:2,MAR:2,APR:4,MAY:5,JUN:6,JUL:7,AUG:8,SEP:9,OCT:10,NOV:11, DEC:12}; Month1 = months [strMonth1.toUpperCase()]; Month2 = months [strMonth2.toUpperCase()]; if(Month1> Month2) { strDay1 = strDay1 +''+(new Date()。getFullYear()); strDay2 = strDay2 +''+(new Date()。getFullYear()+ 1); } else { strDay1 = strDay1 +''+(new Date()。getFullYear()); strDay2 = strDay2 +''+(new Date()。getFullYear( )); } var day = 1000 * 60 * 60 * 24; var d1 = Date.parse(strDay1); var d2 = Date.parse(strDay2); var diff = d2 - d1;返回Math.round(差异/天) } Use The Following Function in Javascript.function getDays(strDay1, strDay2) { debugger; var strMonth1 = strDay1.split(' ')[0]; var strMonth2 = strDay2.split(' ')[0]; var months = { JAN: 1, FEB: 2, MAR: 2, APR: 4, MAY: 5, JUN: 6, JUL: 7, AUG: 8, SEP: 9, OCT: 10, NOV: 11, DEC: 12 }; Month1 = months[strMonth1.toUpperCase()]; Month2 = months[strMonth2.toUpperCase()]; if (Month1 > Month2) { strDay1 = strDay1 + ' ' + (new Date().getFullYear()); strDay2 = strDay2 + ' ' + (new Date().getFullYear()+1); } else { strDay1 = strDay1 + ' ' + (new Date().getFullYear()); strDay2 = strDay2 + ' ' + (new Date().getFullYear()); } var day = 1000 * 60 * 60 * 24; var d1 = Date.parse(strDay1); var d2 = Date.parse(strDay2); var diff = d2 - d1; return Math.round(diff / day) } 这篇关于如何计算javascript中两个日期之间的天数? (没有年限,每年只有一个月和一天)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 08:29