js点击上一周获取上N周的日期和对应的星期,击下一周获取下N周的日期和对应的星期

js点击上一周获取上N周的日期和对应的星期,击下一周获取下N周的日期和对应的星期,第1张

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Document</title>

<style>

td{

border:1px solid;

}

button{

margin: 5px;

}

</style>

<script>

windowonload = function(){

var cells = documentgetElementById('monitor')getElementsByTagName('td');

var clen = cellslength;

var currentFirstDate;

var formatDate = function(date){

var year = dategetFullYear()+'年';

var month = (dategetMonth()+1)+'月';

var day = dategetDate()+'日';

var week = '('+['星期天','星期一','星期二','星期三','星期四','星期五','星期六'][dategetDay()]+')';

return year+month+day+' '+week;

};

var addDate= function(date,n){

datesetDate(dategetDate()+n);

return date;

};

var setDate = function(date){

var week = dategetDay()-1;

date = addDate(date,week-1);

currentFirstDate = new Date(date);

for(var i = 0;i<clen;i++){

cells[i]innerHTML = formatDate(i==0  date : addDate(date,1));

}

};

documentgetElementById('last-week')onclick = function(){

setDate(addDate(currentFirstDate,-7));

};

documentgetElementById('next-week')onclick = function(){

setDate(addDate(currentFirstDate,7));

};

setDate(new Date());

}

</script>

</head>

<body>

<button id="last-week">上一周</button><button id="next-week">下一周</button>

<table id="monitor">

<tr>

<td></td>

<td></td>

<td></td>

<td></td>

<td></td>

<td></td>

<td></td>

</tr>

</table>

</body>

</html>

JavaScript获取当前日期时间同时显示星期几,具体代码如下:

<html>

<head>

<meta

>

function GetWeekDate(time){

var now = new Date(time); //当前日期

thisnowDayOfWeek = nowgetDay(); //今天本周的第几天

thisnowYear = nowgetYear(); //当前年

thisnowMonth = nowgetMonth(); //月

thisnowDay = nowgetDate(); //日

thisbeginHour="09:00:00";

thisendHour="23:59:59";

thisnowYear += (thisnowYear < 2000) 1900 : 0; //

thisnowDayOfWeek = thisnowDayOfWeek==07:thisnowDayOfWeek; // 如果是周日,就变成周七

}

GetWeekDateprototypedate2str=function(x,y){//date2str(new Date(curTime),"yyyy-MM-dd")

var z ={y:xgetFullYear(),M:xgetMonth()+1,d:xgetDate(),h:xgetHours(),m:xgetMinutes(),s:xgetSeconds()};

return yreplace(/(y+|M+|d+|h+|m+|s+)/g,function(v) {return ((vlength>1"0":"")+eval('z'+vslice(-1)))slice(-(vlength>2vlength:2))});

}

GetWeekDateprototypeformatDate=function(date){//格局化日期:yyyy-MM-dd

var myyear = dategetFullYear();

var mymonth = dategetMonth()+1;

var myweekday = dategetDate();

//alert("formatDate"+myyear+":"+mymonth+":"+myweekday)

if(mymonth < 10){

mymonth = "0" + mymonth;

}

if(myweekday < 10){

myweekday = "0" + myweekday;

}

return (myyear+"-"+mymonth + "-" + myweekday);

}

GetWeekDateprototypegetWeekStartDate=function(){ //获得本周的开端日期

var weekStartDate = new Date(thisnowYear, thisnowMonth, thisnowDay - thisnowDayOfWeek+1);

return thisformatDate(weekStartDate);

}

GetWeekDateprototypegetWeekEndDate=function(){//获得本周的停止日期

var weekEndDate = new Date(thisnowYear, thisnowMonth, thisnowDay + (6 - thisnowDayOfWeek+1));

return thisformatDate(weekEndDate);

}

GetWeekDateprototypegetAWeedkYMD=function(){//获得本周周一~周日的年月日

var ymdArr=[];

for (var i = 0; i < 7; i++) {

ymdArr[i]=[];

//ymdArr[i][0]=thisformatDate(new Date(thisnowYear, thisnowMonth, thisnowDay - thisnowDayOfWeek+i+1));

ymdArr[i][0]=thisdate2str(new Date(thisnowYear, thisnowMonth, thisnowDay - thisnowDayOfWeek+i+1),'yyyy-MM-dd');

ymdArr[i][1]=thisdate2str(new Date(thisnowYear, thisnowMonth, thisnowDay - thisnowDayOfWeek+i+1), 'MM月dd日');

};

return ymdArr;

}

GetWeekDateprototypegetQishu=function(time){//获得本周是指定日期(712日)的第几周

var oNDate=new Date(time); //系统当前时间

var oEDate=new Date('2015/7/12 23:59:59');

var diff= oNDategetTime()-oEDategetTime();

//consolelog(diff)

//consolelog("相差天:"+diff/(2460601000))

//consolelog("相差期数:"+parseInt(diff/(2460601000))/7)

//consolelog("取整期数:"+Mathfloor(parseInt(diff/(2460601000))/7))

return (diff/(2460601000))/7;

}

GetWeekDateprototypegetWeeksDates=function(time){//获取历史周排行的周一到周日时间段

var qishu = (thisqishu || thisqishu==0)thisqishu:thisgetQishu(time);

//var qishu=thisgetQishu(time);

var WeeksTimes=[];//存放时间的二维数组

minYear=2015,

minMonth=7,

minDay=12;

for (var i = 0; i<qishu; i++) {

var sday;

var eday;

WeeksTimes[i]=[];

if(i==0){//如果离2015-07-12只相差1期

sday=minDay+1; //开始时间+1天

}else{//如果离2015-07-12相差期数>1

sday=minDay+1+7i;

}

eday=minDay+7(i+1);//结束时间+7天

WeeksTimes[i][0]=thisformatDate(new Date(minYear,minMonth-1,sday))+" "+thisbeginHour;

WeeksTimes[i][1]=thisformatDate(new Date(minYear,minMonth-1,eday))+" "+thisendHour;

};

//如果是一周的周日就不要减去一期

return WeeksTimes;

}

GetWeekDateprototypetodayData=function(json){//处理tender_list_weekjsp页面

var oQishu=$('qishu');

iQishu=thisqishu;//期数+1是因为,相差0期就是第1期

oThisWeekListBtn=$('total_list_btn'),//查看本周排行榜按钮

sTime=thisgetWeekStartDate()+" "+thisbeginHour,

eTime=thisgetWeekEndDate()+" "+thisendHour;

//1、修改期数

//consolelog("相差周:"+iQishu);

if(/^[1-9]\d$/test(iQishu) || iQishu==0){//整数 +3

oQishuhtml(iQishu1+3);

}else{

oQishuhtml(Mathfloor(iQishu)1+4);

}

oThisWeekListBtnattr("onclick","showWeekList('"+sTime+"','"+eTime+"')");//2、修改查看本周排行榜的起止时间

}

GetWeekDateprototypehistoryData=function(time){//处理tender_list_weekjsp页面

var oQishu=$('qishu'),

oDateList=$('#dateList'), //顶部导航时间

oHistoryList=$('#history_cont'),//历史周

aThisWeekYMD=thisgetAWeedkYMD(),//本周周一~周日的年月日

aThisWeekDates=thisgetWeeksDates(time);//获取历史周排行的周一到周日时间段

iQishu=thisqishu;

//consolelog("相差周:"+iQishu);

//1、修改期数,+4是加上前3期,在+1是如果间隔3期,当前就是第4期

//如果时间23:59:59是正整数(-1),如果是0整数(3),如果是非整数(向下取整),或负数(向上取整)但是负数不考虑

//oQishuhtml(iQishu1+3);

if(/^[1-9]\d$/test(iQishu) || iQishu==0){//整数 +3

oQishuhtml(iQishu1+3);

}else{

oQishuhtml(Mathfloor(iQishu)1+4);

}

//2、给顶部时间追加时间

for (var i = 0; i < aThisWeekYMDlength; i++) {

var str='<li data-time="'+aThisWeekYMD[i][0]+'">'+aThisWeekYMD[i][1]+'</li>';

$(str)appendTo(oDateList);

};

//3、给历史周排行榜添加周期数

if(thisqishu>1){//如果相差的期数大于1期

//consoledir(aThisWeekDates)

for (var j = 0; j < aThisWeekDateslength-1; j++) {

var iQiNum=j+4;

var str='<li onclick="showWeekList(\''+aThisWeekDates[j][0]+'\',\''+aThisWeekDates[j][1]+'\')">第'+iQiNum+'期</li>';

$(str)prependTo(oHistoryList);

};

}

}

GetWeekDateprototypeinit=function(time){

var iQishu=thisgetQishu(time),//期数+1是因为,相差0期就是第1期

json={};

jsonqishu=iQishu;

thisqishu=iQishu;

if($('#pageType')val()=="today"){//如果是tender_list_weekjsp页面

if(new Date(time)getTime() < new Date('2015/07/12 23:59:59')getTime()){//特殊处理时间小于20150712

$('qishu')html('3');

$('total_list_btn')attr("onclick","showWeekList('2015-07-03 09:00:00','2015-07-12 23:59:59')");

return false;

}

thistodayData(json);

}else if($('#pageType')val()=="history"){

if(new Date(time)getTime() < new Date('2015/07/12 23:59:59')getTime()){//特殊处理时间小于20150712

$('qishu')html('3');

$('#dateList')addClass('dateList2')html('<li data-time="2015-07-03">07月03日</li><li data-time="2015-07-04">07月04日</li><li data-time="2015-07-05">07月05日</li><li data-time="2015-07-06">07月06日</li><li data-time="2015-07-07">07月07日</li><li data-time="2015-07-08">07月08日</li><li data-time="2015-07-09">07月09日</li><li data-time="2015-07-10">07月10日</li><li data-time="2015-07-11">07月11日</li><li data-time="2015-07-12">07月12日</li>');

$('#history_cont')html('<li onclick="showWeekList(\'2015-06-26 09:00:00\',\'2015-07-02 23:59:59\')">第二期</li><li onclick="showWeekList(\'2015-06-19 12:00:00\',\'2015-06-25 23:59:59\')">第一期</li>');

return false;

}

thishistoryData(time);

}

//consoledir(thisgetWeeksDates(time));

};

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 10 Transitional//EN" ">

一、只显示第几周用这个,打开记事本,把下面代码复制粘贴,然后另存为“第几周html”即可看到自动d出本周为36周。

以下代码保存为htm文件,用ie打开此文件即可

=================

<html>

<body>

<script language="vbscript">

msgbox DateDiff("ww",year(date) & "-1-1",date,1,1)

</script>

</body>

</html>

================

二、输入年,第几周计算,自动计算这周的起始和终止日期

把下面代码复制粘贴在记事本内,另存为:第几周起始日期html,用ie打开即可

==============

<!javascript:>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 40 Transitional//EN">

<HTML>

<HEAD>

<TITLE> New Document </TITLE>

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">

function showDateCount(year,weekNum){

var date=new Date(year,0,(7weekNum));

var FD=new Date(dategetYear(),(dategetMonth()),(dategetDate()-dategetDay()+1))

var Monday=FDgetYear()+"-"+(FDgetMonth()+1)+"-"+FDgetDate()//一周的第一天

var LD=new Date(dategetYear(),(dategetMonth()),(dategetDate()-dategetDay()+7))

var Sunday=LDgetYear()+"-"+(LDgetMonth()+1)+"-"+LDgetDate() //一周的最后一天

alert(year+"年第"+weekNum+"周是"+Monday+"至"+Sunday)

}

</SCRIPT>

<META NAME="Generator" CONTENT="EditPlus">

<META NAME="Author" CONTENT="">

<META NAME="Keywords" CONTENT="">

<META NAME="Description" CONTENT="">

</HEAD>

<BODY>

<input type="text" value="2007" size="20" name="year">年<input type="text" name="weekNum"

value="30" size="20">

<input type="button" name="GO" value="Click" onClick="showDateCount

(yearvalue,weekNumvalue)">

</BODY>

</HTML>

========================================

也可以把两个 合在一起,楼上的答案是一个日历,也可以加在一起使用。

我修改的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 10 Transitional//EN" ">

<!DOCTYPE html>

<html>

<head>

<script src=";

实现效果这样?:

今天是2018/6/12 下午1:33:05 星期二

以上就是关于js点击上一周获取上N周的日期和对应的星期,击下一周获取下N周的日期和对应的星期全部的内容,包括:js点击上一周获取上N周的日期和对应的星期,击下一周获取下N周的日期和对应的星期、js显示当前日期时间和星期几、如何获取本周的起止日期等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/10158235.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存