如何弄清楚JavaScript输出一年所有周的方法。
<!DOCTYPE >
<html >
<head>
<title>JavaScript Sample</title>
</head>
<body>
<script>
var yugi = function(year) {
var d = new Date(year, 0, 1);
while (d.getDay() != 1) {
d.setDate(d.getDate() + 1);
}
var to = new Date(year + 1, 0, 1);
var i = 1;
for (var from = d; from < to;) {
document.write(year + "年第" + i + "周 "
+ (from.getMonth() + 1) + "月" + from.getDate() + "日 - ");
from.setDate(from.getDate() + 6);
if (from < to) {
document.write((from.getMonth() + 1) + "月" + from.getDate() + "日<br / >");
from.setDate(from.getDate() + 1);
} else {
to.setDate(to.getDate() - 1);
document.write((to.getMonth() + 1) + "月" + to.getDate() + "日<br / >");
}
i++;
}
}
yugi(2015);
</script>
</body>
</html>Date 对象
Date 对象用于处理日期和时间。
创建 Date 对象的语法:
var myDate=new Date()
注释:Date 对象会自动把当前日期和时间保存为其初始值。
getDay() 方法
getDay() 方法可返回表示星期的某一天的数字。
语法:
dateObject.getDay()
返回值
dateObject 所指的星期中的某一天,使用本地时间。返回值是 0(周日) 到 6(周六) 之间的一个整数。








网友评论文明上网理性发言 已有0人参与
发表评论: