javascript高手 速进

来源:百度知道 编辑:UC知道 时间:2024/05/26 02:24:43
想用 JavaScript 获得本月的第一天和最后一天 以及本周的第一天和最后一天 请问该如何做 (每个月的天数不是确定的)

<script>
//获取指定年月的天数
function getMonthDays(yy, mm) {
return new Date(yy, mm, 0).getDate();
}
function getDate(yy,mm,dd){
return new Date(yy,mm,dd);
}
var date = new Date();
var lastday = getMonthDays(date.getYear(),date.getMonth()+1); //得到本月最大的日期
var firstStr = (date.getMonth()+1)+"月1日-"+(date.getMonth()+1)+"月"+lastday+"日";
var secondStr = getDate(date.getYear(),date.getMonth(),date.getDate()-date.getDay()); //得到本周的第一天
date = secondStr;
var secondStr1 = getDate(date.getYear(),date.getMonth(),date.getDate()+6); //得到本周的最后一天
document.write("本月从 "+firstStr+" ");
document.write("本周从 "+(secondStr.getMonth()+1)+"月"+secondStr.getDate()+"日-");
document.write((secondStr1.getMonth()+1)+"月"+secondStr1.getDate()+"日");
</script>