日期:2014-05-16 浏览次数:20413 次
$(document).ready(function() { var myDate = new Date(); var yearNow = myDate.getFullYear(); var monthNow = myDate.getMonth() + 1; var dayNow = myDate.getDate(); var weekIndex = GetWeekIndex(myDate); document.getElementById("year").innerHTML = yearNow; document.getElementById("month").innerHTML = monthNow; document.getElementById("day").innerHTML = dayNow; document.getElementById("weekIndex").innerHTML = weekIndex; });
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <script type="text/javascript" src="jquery.js"></script> </head> <body> <script type="text/javascript"> $(function() { var myDate = new Date(); var yearNow = myDate.getFullYear(); var monthNow = myDate.getMonth() + 1; var dayNow = myDate.getDate(); var weekIndex = GetWeekIndex(myDate); $('#year').html(yearNow); $('#month').html(monthNow); $('#day').html(dayNow); $('#weekIndex').html(weekIndex); }); function GetFirstWeekBegDay(year) { var tempdate = new Date(year, 0, 1); var temp = tempdate.getDay(); temp = temp == 0 ? 7 : temp; tempdate = tempdate.setDate(tempdate.getDate() + (8 - temp)); return new Date(tempdate); } function GetWeekIndex(dateobj) { var firstDay = GetFirstWeekBegDay(dateobj.getFullYear()); if (dateobj < firstDay) { firstDay = GetFirstWeekBegDay(dateobj.getFullYear() - 1); } d = Math.floor((dateobj.valueOf() - firstDay.valueOf()) / 86400000); return Math.floor(d / 7) + 1; } </script> <span style="color: red;" id="year"></span>年 <span style="color: red;" id="month"></span>月 <span style="color: red;"id="day"></span>日 第<span style="color: red;" id="weekIndex"></span>周 </body> </html>