刚学javascript遇到的问题——怎么算…

本文提供了一段JavaScript代码,用于确定当前日期所在的一周起始与结束日期,同时计算出当前是哪一周。通过定义`weekNo`、`formatDate`及`firstAndFinalDayStr`等函数,实现了周信息的精确获取。
function weekNo() {
  var totalDays = 0;
  now = new Date();
  years=now.getYear()
  if (years < 1000)
  years+=1900
  var days = new Array(12);
  days[0] = 31;
  days[2] = 31;
  days[3] = 30;
  days[4] = 31;
  days[5] = 30;
  days[6] = 31;
  days[7] = 31;
  days[8] = 30;
  days[9] = 31;
  days[10] = 30;
  days[11] = 31;
   if (Math.round(now.getYear()/4) == now.getYear()/4) {
     days[1] = 29
  }else{
     days[1] = 28
  }

  if (now.getMonth() == 0) {
     totalDays = totalDays + now.getDate();
  }else{
     var curMonth = now.getMonth();
     for (var count = 1; count <= curMonth; count++) {
         totalDays = totalDays + days[count - 1];
     }
     totalDays = totalDays + now.getDate();
   }
   var week = Math.round(totalDays/7);
   return week;
}

function formatDate(date) {

return date.getFullYear() + "." + date.getMonth() + "." + date.getDate();
}

function firstAndFinalDayStr() {
var now = new Date();
var weeoDay = now.getUTCDay();
var firstDay = new Date();
firstDay.setDate(now.getDate() - weeoDay + 1);

var finalDay = new Date();
finalDay.setDate(now.getDate() + (7 - weeoDay));

return formatDate(firstDay) +"~" + formatDate(finalDay)+" "
}

function run() {
var info = "", no = weekNo();
info += firstAndFinalDayStr();

info += document.getElementByIdx_x("weekInfo").innerHTML = info;

}
run();
document.write("第 "+weekNo()+"周("+years+"年)")


// -->
看目前网上的时间显示,并没有这种显示的代码,写出来,希望能给大家更多的帮助。
更多信息,请点击:http://blog.sina.com.cn/u/2308368145
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值