var date = new Date();
datesetDate(dategetDate()-dategetDay()+1);
$("#year option:contains("+dategetFullYear()+")")prop("selected",true);
$("#month option")eq(dategetMonth()+1)prop("selected",true);
$(#day option")eq(dategetDate())prop("selected",true);
datesetDate(dategetDate()+7);
$("#year1 option:contains("+dategetFullYear()+")")prop("selected",true);
$("#month1 option")eq(dategetMonth()+1)prop("selected",true);
$(#day1 option")eq(dategetDate())prop("selected",true);
顺便说一句:现在通常的做法(也是国际惯例),是把星期日作为一周的开始,而星期六则是周末,如果你想这么做,只需把第二行后面的+1去掉即可。
function dayList(len){
var d = '日一二三四五六';
var sDay = new Date()getDay();
var result = [];
var i = 0;
while(i<len){
var tmp = sDay+i ;
if(!((sDay+i) <7)){
tmp = (sDay+i)%7
}
resultpush('周'+dcharAt(tmp));
i++;
}
return result;
}
consolelog(dayList(17));
你好,
其实题设还少一个条件,我们通常所谓的第几周,是相对于某一年的。因此应该还有一个变量(年)。
好了,条件齐全了我们来看一下代码:
// 变量第几周var week = 3;
// 假设是2017年
var year = 2017;
// 一天时间的毫秒时长
var daySeconds = 24 3600 1000;
// 指定年份第一天的日期对象,即:一月一日
var firstDayOfYear = new Date(year + '/01/01');
/
计算第一周的第一天日期对象
通过当年的第一天取得是星期几,用当年的第一天减去第一周已经过去的几天,得到第一周的第一天
/
var firstDayOfFirstWeek = new Date(firstDayOfYeargetTime() - firstDayOfYeargetDay() daySeconds);
/
计算指定第week周的第一天
week-1:第week周,区间应该是第(week - 1)周结束后开始算起
7:一周有7天
/
var firstDayOfThisWeek = new Date(firstDayOfFirstWeekgetTime() + (week - 1) 7 daySeconds);
/
计算指定第week周的最后一天
6:一周共7天,开始那一天加上6天得到最后一天
/
var lastDayOfThisWeek = new Date(firstDayOfThisWeekgetTime() + 6 daySeconds);
var startDayStr = firstDayOfThisWeekgetFullYear() + '-' + (firstDayOfThisWeekgetMonth() + 1) + '-' + firstDayOfThisWeekgetDate();
var endDayStr = lastDayOfThisWeekgetFullYear() + '-' + (lastDayOfThisWeekgetMonth() + 1) + '-' + lastDayOfThisWeekgetDate();
consolelog('2017年第' + week + '周:' + startDayStr + '——' + endDayStr);
重要:国标每周第一天为周日,如果按国内每周第一天为周一算,计算日期结果应加一天。
希望是你想要的结果,如有疑问可追问。
var date = new Date();
var day = dategetDay();
var hours = dategetHours();
if((day == 5 && hours >= 16) || day == 6 || day == 0){
alert('OK')
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 40 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript">
var timeString;
function tick() {
var year, month, day, week, hours, minutes, seconds, ap;
var intYear, intMonth, intWeek, intDay, intHours, intMinutes, intSeconds;
var today;
today = new Date();
intYear = todaygetFullYear();
intMonth = todaygetMonth() + 1;
intDay = todaygetDate();
intHours = todaygetHours();
intMinutes = todaygetMinutes();
intSeconds = todaygetSeconds();
intWeek = todaygetDay();
//获取系统时间的小时数
if (intHours == 0) {
hours = intHours + ":";
ap = "凌晨";
} else if (intHours < 12) {
hours = intHours + ":";
ap = "早晨";
} else if (intHours == 12) {
hours = intHours + ":";
ap = "中午";
} else {
//intHours = intHours - 12;
hours = intHours + ":";
ap = "下午";
}
//获取系统时间的分数
if (intMinutes < 10) {
minutes = "0" + intMinutes + ":";
} else {
minutes = intMinutes + ":";
}
//获取系统时间的秒数
if (intSeconds < 10) {
seconds = "0" + intSeconds + " ";
} else {
seconds = intSeconds + " ";
}
switch (intWeek) {
case 1:
week = "周一";
break;
case 2:
week = "周二";
break;
case 3:
week = "周三";
break;
case 4:
week = "周四";
break;
case 5:
week = "周五";
break;
case 6:
week = "周六";
break;
case 0:
week = "周日";
break;
}
timeString = " "+intYear + '年' + intMonth + '月' + intDay + '日' + week
+ hours + minutes + seconds ;
documentgetElementById("clock")innerHTML = timeString;
//每隔1秒钟执行一次tick函数
windowsetTimeout("tick()",1000);
}
</script>
</HEAD>
<BODY onload="tick();">
<table border="1" width="200">
<tr>
<td id="clock"></td>
</tr>
</table>
</BODY>
</HTML>
以上就是关于js日期下拉框默认为本周周一的日期全部的内容,包括:js日期下拉框默认为本周周一的日期、在html页面用js 获得最近一周、js中如何将每星期换成具体的时间段的方法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)