下个月,然后得到它的日期。由于本月和本年你已经拥有 的数量: var monthdates = new Date(thisyear,(thismonth + 1),0)。 getDate(); 你已经完成了。 但是这不是一塌糊涂,而是这样做的例行程序将会做你所做的事情。 /> 想要,它不是速度的最佳选择,需要''fromDate''比''toDate'更早。 '。夏令时不应该是一个问题,如果你的系统日期是错误的......好吧,我不能从这里解决这个问题。 :-) 它也可以更简洁,但它应该可以完成这项工作。 //传递格式为YYYY-MM-DD的日期字符串, //例如''1994-10-15'' 函数dateDifYMD(ds){ //创建日期对象 - 来自必须更早的日期 var toDate = new Date(); var dsA = ds.split('' - ''); var fromDate = new Date(+ dsA [ 0],+ dsA [1] -1,+ dsA [2]); //获取两个日期的月数 var toMonths = toDate .getFullYear()* 12 + toDate.getMonth(); var fromMonths = fromDate.getFullYear()* 12 + fromDate.getMonth(); //在几个月内获得好处 var months = toMonths - fromMonths; //从月份减去月份,如果走得太远则修复 toDate.setMonth(toDate.getMonth() - months); if(toDate< fromDate){ toDate.setMonth(toDate.getMonth() +1); - 月; } //计算整年和月数 var years = Math.floor(月/ 12); 个月=月%12; //或月数=月数 - 年* 12; //逐个减去天数,直到走得太远 var days = 0; while(toDate> fromDate){ toDate.setDate(toDate.getDate() - 1); ++天; } //调整太远 --days; //格式返回字符串 var rString = years +''year''+((years!= 1)?'s'':'''')+'','' +月+''月''+((月!= 1)?'''':'''')+'','' +天+''天' '+((days!= 1)?'s'':''''); 返回rString; } alert(dateDifYMD('''1994-10-15'')); [...] - RobA much more concise routine is to set a date of a date object to the 0thof next month, then get its date. Since you already have the number ofthis month and the year:var monthdates = new Date(thisyear,(thismonth + 1),0).getDate();And you''re done.But rather than mess with that, here''s a routine that will do what youwant, it isn''t optimal for speed and needs the ''fromDate'' to be earlierthan the ''toDate''. Daylight saving should not be an issue and if yoursystem date is wrong... well, I can''t fix that from here. :-)It could also be more concise, but it should do the job.// Pass a date string with format YYYY-MM-DD,// e.g. ''1994-10-15''function dateDifYMD(ds){// Create from and to date objects - from must be earlier datevar toDate = new Date();var dsA = ds.split(''-'');var fromDate = new Date(+dsA[0], +dsA[1]-1, +dsA[2]);// Get month counts for both datesvar toMonths = toDate.getFullYear()*12 + toDate.getMonth();var fromMonths = fromDate.getFullYear()*12 + fromDate.getMonth();// Get difference in monthsvar months = toMonths - fromMonths;// Subtract months from toDate months, fix if goes too fartoDate.setMonth(toDate.getMonth() - months);if (toDate < fromDate) {toDate.setMonth(toDate.getMonth()+1);--months;}// Calc whole years and monthsvar years = Math.floor(months/12);months = months%12; // or months = months - years*12;// Subtract days one by one until gone too farvar days=0;while (toDate > fromDate) {toDate.setDate(toDate.getDate()-1);++days;}// Adjust for going too far--days;// Format return stringvar rString = years + ''year'' + ((years != 1)?''s'':'''') + '', ''+ months + ''month'' + ((months != 1)?''s'':'''') + '', ''+ days + ''day'' + ((days != 1)?''s'':'''');return rString;}alert(dateDifYMD(''1994-10-15''));[...]--Rob 除了RobG为您提供的帮助外,我还想告诉您 我试着写一个脚本我也觉得我有义务在我的初步(虽然可以解释)后做出反应。 但几分钟后我意识到这个任务*没有确切的 数学解*。计算两个日期之间 毫秒的数量是一个琐事。因此,计算 的秒数,分钟数,小时数和天数是一个琐事。 但你不能说在哪里正好是X年,Y个月和Z天 从那时起。 X是几年?标准,飞跃,3标准+ 1飞跃? Y月份是多少?包括30天,29天,30 + 31对? 所以唯一的*可计算*答案可以是:Z天以来在哪里 然后。 PS除非我遗漏了一些东西,并且有一些共同的国际协议。Besides the great help RobG provided to you, I wanted to tell you thatI tried to write a script for you too as I felt obligated after myinitial (though explicable) reaction.But few minutes later I realized that this task *doesn''t have an exactmathematical solution*. It is a trivia to calculate the amount ofmilliseconds between two dates. So it''s a trivia to calculate theamount of seconds, minutes, hours and days.But you cannot say "where are exactly X years, Y months and Z dayssince then".X of what years? Standard, leap, 3 standard + 1 leap ?Y of what month? Consisting of 30 days, 29 days, 30+31 pairs?So the only *calculatable* answer can be: "where are Z days sincethen".P.S. Unless I''m missing something and there are some commoninternational agreements about this. 这篇关于计数脚本有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-13 15:58