js 获取某年某月有几周,以及每周的周一和周末是几号到几号
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 获取某年某月有几周,以及每周的周一和周末是几号到几号相关的知识,希望对你有一定的参考价值。
出现跨月的情况,从第一个 周一开始算第一周,前面的算是上个月的最后一周,最后一周不满用下个月补上
java 实现也行
function getInfo(year, month)
var d = new Date();
// what day is first day
d.setFullYear(year, month-1, 1);
var w1 = d.getDay();
if (w1 == 0) w1 = 7;
// total day of month
d.setFullYear(year, month, 0);
var dd = d.getDate();
// first Monday
if (w1 != 1) d1 = 7 - w1 + 2;
else d1 = 1;
week_count = Math.ceil((dd-d1+1)/7);
document.write(year + "年" + month + "月有" + week_count +"周<br/>");
for (var i = 0; i < week_count; i++)
var monday = d1+i*7;
var sunday = monday + 6;
var from = year+"/"+month+"/"+monday;
var to;
if (sunday <= dd)
to = year+"/"+month+"/"+sunday;
else
d.setFullYear(year, month-1, sunday);
to = d.getFullYear()+"/"+(d.getMonth()+1)+"/"+d.getDate();
document.write("第"+(i+1)+"周 从" + from + " 到 " + to + "<br/>");
getInfo(2013,12);
</script> 参考技术A <h1><span id="year"></span>年月份周数</h1>
<script type="text/javascript">
var time = new Date();
var year = time.getFullYear();
var countweek;
document.getElementById('year').innerhtml = year;
for(var i = 0 ; i < 12; i++)
countweek = 0;
var mondays;
//判断月份的天数
if(i == 0 ||i == 2 ||i == 4 ||i == 6 ||i == 7 ||i == 9 ||i == 11)
mondays = 31;
if(i == 3 ||i == 5 ||i == 8 ||i == 10)
mondays = 30;
if(i == 1)
if((year%4 == 0 && year%100 != 0) || year%400 == 0)
mondays = 28;
else
mondays = 29;
for(var j = 1 ; j < ( mondays + 1) ; j++ )
time.setMonth(i,j);
var date = time.getDay();
if(date == 1)
countweek = countweek + 1;
document.write(( i + 1 ) + '月有 ' + countweek + ' 周 , ' );
</script>
好问题,通过这个问题,我知道了很多东西。这个代码也是我花了几个小时研究写出来的,我还写了显示日历那样的,还在想用JS写个日历牌出来,但是现在还没想到。一起学习,一起进步!思路清晰了的话,很好懂。 参考技术B 看看js的Date()对象参考手册吧,够你完成功能的了追问
求详细代码,大神
追答for语句一天一天的查呗,还能怎样
或者你也可以找到第一个星期一,然后计算
Date()对象是可以返回星期的
PHP获取一年有几周以及每周开始日期和结束日期
代码一:
<?php header("Content-type:text/html;charset=utf-8"); date_default_timezone_set("Asia/Shanghai"); $year = (int)$_GET[‘year‘]; $week = (int)$_GET[‘week‘]; $weeks = date("W", mktime(0, 0, 0, 12, 28, $year)); echo $year . ‘年一共有‘ . $weeks . ‘周<br />‘; if ($week > $weeks || $week <= 0) { $week = 1; } if ($week < 10) { $week = ‘0‘ . $week; } $timestamp[‘start‘] = strtotime($year . ‘W‘ . $week); $timestamp[‘end‘] = strtotime(‘+1 week -1 day‘, $timestamp[‘start‘]); echo $year . ‘年第‘ . $week . ‘周开始时间戳:‘ . $timestamp[‘start‘] . ‘<br />‘; echo $year . ‘年第‘ . $week . ‘周结束时间戳:‘ . $timestamp[‘end‘] . ‘<br />‘; echo $year . ‘年第‘ . $week . ‘周开始日期:‘ . date("Y-m-d", $timestamp[‘start‘]) . ‘<br />‘; echo $year . ‘年第‘ . $week . ‘周结束日期:‘ . date("Y-m-d", $timestamp[‘end‘]); ?>
代码二:
<?php header("Content-type:text/html;charset=utf-8"); function getIsoWeeksInYear($year) { $date = new DateTime; $date->setISODate($year, 53); return ($date->format("W") === "53" ? 53 : 52); } function weekday($custom_date) { $week_start = date(‘d-m-Y‘, strtotime(‘this week monday‘, $custom_date)); $week_end = date(‘d-m-Y‘, strtotime(‘this week sunday‘, $custom_date)); $week_array[0] = $week_start; $week_array[1] = $week_end; return $week_array; } echo ‘<br> Weeks in 2013<br>‘ . getIsoWeeksInYear(2013); $weekday = weekday(strtotime(date(‘d-m-Y‘, strtotime(‘5-8-2013‘)))); echo ‘<br> 10-8-2013‘; echo ‘<br>Start: ‘ . $weekday[0]; echo ‘<br>End: ‘ . $weekday[1]; ?>
以上是关于js 获取某年某月有几周,以及每周的周一和周末是几号到几号的主要内容,如果未能解决你的问题,请参考以下文章