您的位置:首页 > Web前端 > JavaScript

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

2016-06-30 17:40 441 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: