huaixiaozi2008 2009-10-22 18:52
浏览 138
已采纳

help!js计算时间

问题:例如:现在时间是:2009年10月22号 ,2年后的是:2011年10月22号 求:这2年期间一共24个月 ,每个月的天数?
如:2009年10月22号 到2009年11月22号是31天,
2009年11月22号 到2009年12月22号是30天.。。。
用js求出来:

  • 写回答

4条回答 默认 最新

  • 成尚謙 2009-10-22 22:34
    关注

    试试这个:
    [code="javascript"]
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">





    Month's Dates /*获取与fromdate相差monthCount个月的日期*/ function getDistMonths(fromdate,monthCount){ var resultDate,year,month,date; year = fromdate.getFullYear(); date = fromdate.getDate(); var month = fromdate.getMonth()+1+monthCount; var c = parseInt((month-1)/12); month = month%12 || 12; year += c; resultDate = year + '-'+month+'-'+date; return resultDate; } /*接受YYYY-MM-DD的日期字符串参数,返回两个日期相差的天数*/ function getDistDates(startDate,endDate) { var startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime(); var endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime(); var dates = Math.abs((startTime - endTime))/(1000*60*60*24); return '共' + dates+ '天'; } /*计算每个月的天数,以数组返回*/ function countMonthDates(months,date) { var results = [];//结果 var _today = date || new Date();//如果不给定起算时间则从今天算起 var _months = months || 24;//如果不给定计算的月数则计算未来两年里面的24月 var startDate = getDistMonths(_today,0);//获得起算日期的YYYY-MM-DD格式的字符串日期 for(var i = 1; i <= _months; i++) { var nextMonthDate = getDistMonths(_today,i);//每个月的截至日期 var o = {}; o.desc = startDate + ' 至 ' + nextMonthDate; o.date = getDistDates(startDate,nextMonthDate);//返回天数 results.push(o); startDate = nextMonthDate;//当前月的结束日期作为下一个月的起始日期 } return results; } window.onload = function() { /*使用举例*/ var strRes = ''; var results = countMonthDates(24,new Date()); if(results.length) { for(var i = 0; i < results.length; i++) { var o = results[i]; //alert(o.desc + ' : ' + o.date); strRes += (o.desc + ' : ' + o.date + '<br/>'); } document.body.innerHTML += strRes; } }







    [/code]

    [code="输出的结果"]
    2009-10-22 至 2009-11-22 : 共31天
    2009-11-22 至 2009-12-22 : 共30天
    2009-12-22 至 2010-1-22 : 共31天
    2010-1-22 至 2010-2-22 : 共31天
    2010-2-22 至 2010-3-22 : 共28天
    2010-3-22 至 2010-4-22 : 共31天
    2010-4-22 至 2010-5-22 : 共30天
    2010-5-22 至 2010-6-22 : 共31天
    2010-6-22 至 2010-7-22 : 共30天
    2010-7-22 至 2010-8-22 : 共31天
    2010-8-22 至 2010-9-22 : 共31天
    2010-9-22 至 2010-10-22 : 共30天
    2010-10-22 至 2010-11-22 : 共31天
    2010-11-22 至 2010-12-22 : 共30天
    2010-12-22 至 2011-1-22 : 共31天
    2011-1-22 至 2011-2-22 : 共31天
    2011-2-22 至 2011-3-22 : 共28天
    2011-3-22 至 2011-4-22 : 共31天
    2011-4-22 至 2011-5-22 : 共30天
    2011-5-22 至 2011-6-22 : 共31天
    2011-6-22 至 2011-7-22 : 共30天
    2011-7-22 至 2011-8-22 : 共31天
    2011-8-22 至 2011-9-22 : 共31天
    2011-9-22 至 2011-10-22 : 共30天
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向