当前位置:asp编程网>技术教程>Javascript教程>  正文

获取年月日周的js函数

2012-07-20 16:40:14   来源:    作者:佚名   浏览量:2250   收藏
<!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>日&nbsp;&nbsp;&nbsp;&nbsp;
  第<span style="color: red;" id="weekIndex"></span>周
</body>
</html>
(鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助)


关于我们-广告合作-联系我们-积分规则-网站地图

Copyright(C)2013-2017版权所属asp编程网