<script>
function getInfo(year, month) {
var d = new Date();
// what day is first day
dsetFullYear(year, month-1, 1);
var w1 = dgetDay();
if (w1 == 0) w1 = 7;
// total day of month
dsetFullYear(year, month, 0);
var dd = dgetDate();
// first Monday
if (w1 != 1) d1 = 7 - w1 + 2;
else d1 = 1;
week_count = Mathceil((dd-d1+1)/7);
documentwrite(year + "年" + month + "月有" + week_count +"周<br/>");
for (var i = 0; i < week_count; i++) {
var monday = d1+i7;
var sunday = monday + 6;
var from = year+"/"+month+"/"+monday;
var to;
if (sunday <= dd) {
to = year+"/"+month+"/"+sunday;
} else {
dsetFullYear(year, month-1, sunday);
to = dgetFullYear()+"/"+(dgetMonth()+1)+"/"+dgetDate();
}
documentwrite("第"+(i+1)+"周 从" + from + " 到 " + to + "<br/>");
}
}
getInfo(2013,12);
</script>
其实我是来混经验的
////////////////////////////////////////////////////////////////////////////////////////////////////
//获取当前日期在当前年第几周函数封装,例如2014-01-10 是当前年的第2周
////////////////////////////////////////////////////////////////////////////////////////////////////
function theWeek() {
var totalDays = 0;
now = new Date();
years = nowgetYear()
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;
//判断是否为闰年,针对2月的天数进行计算
if (Mathround(nowgetYear() / 4) == nowgetYear() / 4) {
days[1] = 29
} else {
days[1] = 28
}
if (nowgetMonth() == 0) {
totalDays = totalDays + nowgetDate();
} else {
var curMonth = nowgetMonth();
for (var count = 1; count <= curMonth; count++) {
totalDays = totalDays + days[count - 1];
}
totalDays = totalDays + nowgetDate();
}
//得到第几周
var week = Mathround(totalDays / 7);
return week;
}
下面是获取当月的第几周
<script language="javascript">var getMonthWeek = function (a, b, c) { / a = d = 当前日期 b = 6 - w = 当前周的还有几天过完(不算今天) a + b 的和在除以7 就是当天是当前月份的第几周 / var date = new Date(a, parseInt(b) - 1, c), w = dategetDay(), d = dategetDate(); return Mathceil( (d + 6 - w) / 7 ); };
var getYearWeek = function (a, b, c) { / date1是当前日期 date2是当年第一天 d是当前日期是今年第多少天 用d + 当前年的第一天的周差距的和在除以7就是本年第几周 / var date1 = new Date(a, parseInt(b) - 1, c), date2 = new Date(a, 0, 1), d = Mathround((date1valueOf() - date2valueOf()) / 86400000); return Mathceil( (d + ((date2getDay() + 1) - 1)) / 7 ); }; today=new Date();//获取当前时间var y = todaygetYear();var m = todaygetMonth()+1;var d = todaygetDate();documentwrite( "今天是",m,"月的第 ", getMonthWeek(y, m, d), " 周" ); </script>
<html xmlns=">
<script>
var d1="201702", d2="201812"; //这两个日期的获取自己搞定
var y1=parseInt(d1substr(0,4));
var m1=parseInt(d1substr(4,2))-1;
var y2=parseInt(d2substr(0,4));
var m2=parseInt(d2substr(4,2))-1;
do{
var d=new Date(y1,m1++,1);
var y=dgetFullYear();
var m=dgetMonth()+1;
var s=y+(m<10"0":"")+m; //这个s就是要打印的日期字串
consolelog(s); //打印方法自己改
}while(d<new Date(y2,m2,1));
</script>
js处理这个事比较麻烦(可以实现,使用date的getTime()方法,做减法后再作除法),我一般使用JAVA来获取:
Calender c=CalendergetInstance();
Date d=cgetTime();
int day=cget(CalenderDAY_OF_YEAR);
以上就是关于js 获取某年某月有几周,以及每周的周一和周末是几号到几号全部的内容,包括:js 获取某年某月有几周,以及每周的周一和周末是几号到几号、js获取当前日期第几周、js中如何获取当年的最后一天等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)