`
lcywjvb
  • 浏览: 60691 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

使用javascript写一个小的程序,通过年月来显示当月中的星期

阅读更多
$(document).ready(function(){
	var start = statform.startTime.value;
	var end = statform.endTime.value;
	if(start!=""&&end!=""){
		var s1 = start.split("-");
		if(s1[1].indexOf("0")==0){
			s1[1] = s1[1].replace("0","");
		}
		var _year = parseInt(s1[0]);
		var _month = parseInt(s1[1]);
		var year = new Date().getYear();
		if(year<1900){
			year = year + 1900;
		}
		if(year <=_year){
			showMonthLink(year,new Date().getMonth());
		}else{
			showMonthLink(_year,11);
		}
	}else{
		var date = new Date();
		var year = date.getYear();
		if(year<1900){
			year = year + 1900;
		}
		var month = date.getMonth();
		showMonthLink(year,month);
	}
});


$(document).ready(function(){
	var start=statform.startTime.value;
	var end=statform.endTime.value;
	if(start!=""&&end!=""){
		var s1 = start.split("-");
		if(s1[1].indexOf("0")==0){
			s1[1] = s1[1].replace("0","");
		}
		if(s1[2].indexOf("0")==0){
			s1[2] = s1[2].replace("0","");
		}
		var _year = parseInt(s1[0]);
		var _month = parseInt(s1[1]);
		var year = new Date().getYear();
		var month = new Date().getMonth();
		if(year == _year && _month-1 == month){
			showWeeks(year,new Date().getMonth());
		}else{
			showWeeks(_year,_month-1);
		}
	}else{
		var date = new Date();
		var year = date.getYear();
		if(year<1900){
			year = year + 1900;
		}
		var month = date.getMonth();
		showWeeks(year,month);
	}
});

function showMonthLink(yea,mon)
{
	var year,month,start,end,back_begin_year,back_begin_month,back_end_year,back_end_month,_b;
	start=statform.startTime.value;
	end=statform.endTime.value;
	if(start!=""&&end!=""){
		var s1 = start.split("-");
		var s2 = end.split("-");
		if(s1[1].indexOf("0")==0){
			s1[1] = s1[1].replace("0","");
		}
		if(s1[2].indexOf("0")==0){
			s1[2] = s1[2].replace("0","");
		}
		if(s2[1].indexOf("0")==0){
			s2[1] = s2[1].replace("0","");
		}
		if(s2[2].indexOf("0")==0){
			s2[2] = s2[2].replace("0","");
		}
		back_begin_year = parseInt(s1[0]);
		back_begin_month = parseInt(s1[1]);
		back_begin_day = parseInt(s1[2]);
		back_end_year = parseInt(s2[0]);
		back_end_month = parseInt(s2[1]);
		back_end_day = parseInt(s2[2]);
	}
	year = parseInt(yea);
	month = parseInt(mon);
	if(month==0&&year==0||year==0)
	{
		var date = new Date();
		year = new Date().getYear();
		month = new Date().getMonth();
		if(year<1900){
			year = year + 1900;
		}
	}
	var str="<table class='weeklink'><tr><td>月份:</td>"
	var date = new Date();
	_year = date.getYear();
	if(_year<1900){
	_year += 1900;
	}
	if(year<_year){
		var difference_year=year-_year;
		if(difference_year==0){
			str=str+"<td><input type='button' onclick='showMonthLink("+_year+","+date.getMonth()+")' value='下一年'/></td>";
		}else if(difference_year<-1){
			str=str+"<td><input type='button' onclick='showMonthLink("+(parseInt(year)+1)+","+month+")' value='下一年' /></td>";
		}else{
			str=str+"<td><input type='button' onclick='showMonthLink("+_year+","+date.getMonth()+")' value='下一年'/></td>";
		}
		
	}
	if(year<2006){
		alert("查询时间不能早于您所设定的时间,必须晚于2005年");
		return;
	}
	realMonth=month+1;
	for(i=0;i<12;i++)
	{
		if(realMonth<=0)
		{
			realMonth=realMonth+12;
			month = month+12;
			year=year-1;
			break;
			if(year<0)
			{
				alert("错误!统计时间不能早于你设定的时间");
				showMonthLink(year+1,11)
			}
		}
		//if(i==2){
			
		//}
		if(year==back_begin_year&&realMonth==back_begin_month&&back_end_month<=realMonth+1)
		{	
			str=str+"<td style='width: 50px;' class='td_poineter' onclick='return showWeeks("+year+","+month+")'><b>"+year+"."+realMonth+"</b></td>";
		}
		else
		{
			str=str+"<td style='width: 50px;' class='td_poineter' onclick='return showWeeks("+year+","+month+")'>"+year+"."+realMonth+"</td>";
		}
		realMonth=realMonth-1;
		month=month-1;
		
	}
	if(month<0){
		month = 11;
		year = year-1;
	}
	str=str+"<td><input type='button' onclick='showMonthLink("+year+","+month+")' value='上一年'/></td></tr></table>";
	document.getElementById("year_month").innerHTML ="";
	document.getElementById("year_month").innerHTML =str;
}
function showWeeks(yea,mon)
{	
	var year,_year,month,str,start,end,back_begin_year,back_begin_month,back_begin_day,back_end_year,back_end_month,back_end_day;
	start=statform.startTime.value;
	end=statform.endTime.value;
	str="<table class='weeklink'><tr><td>星期:</td>";
	year = parseInt(yea);
	month = parseInt(mon);
	if(start!=""&&end!=""){
		var s1 = start.split("-");
		var s2 = end.split("-");
		if(s1[1].indexOf("0")==0){
			s1[1] = s1[1].replace("0","");
		}
		if(s1[2].indexOf("0")==0){
			s1[2] = s1[2].replace("0","");
		}
		if(s2[1].indexOf("0")==0){
			s2[1] = s2[1].replace("0","");
		}
		if(s2[2].indexOf("0")==0){
			s2[2] = s2[2].replace("0","");
		}
		back_begin_year=parseInt(s1[0]);
		back_begin_month=parseInt(s1[1]);
		back_begin_day=parseInt(s1[2]);
		back_end_year=parseInt(s2[0]);
		back_end_month=parseInt(s2[1]);
		back_end_day=parseInt(s2[2]);
	}
	var _year = new Date().getYear();
	if(_year<1900){
		_year = _year + 1900;
	}
	var date = new Date(year,month,1);
	var day_of_week = date.getDay();
	var day = date.getDate();
	month = month+1;
	var weekBeginYear,weekEndYear,weekBeginMonth,weekEndMonth,weekBeginDay,weekEndDay;
	var a=day-day_of_week;
		if(a<=0)
		{
			if(month==2||month==4||month==6||month==9||month==11)
			{
				weekBeginDay=day-day_of_week+31;
				weekEndDay=day+6-day_of_week;
				weekBeginMonth=month-1;
				weekEndMonth=month;
				weekBeginYear=year;
				weekEndYear=year;
				 
			}else if(month==5||month==7||month==8||month==10||month==12)
			{
				weekBeginDay=day-day_of_week+30;
				weekEndDay=day+6-day_of_week;
				weekBeginMonth=month-1;
				weekEndMonth=month;
				weekBeginYear=year;
				weekEndYear=year;
				if(weekBeginMonth<=0)
				{
					weekBeginMonth=weekBeginMonth+12;
					weekBeginYear=weekBeginYear-1;
					if(weekBeginYear<0)
					{
						alert("错误!");
					}
				} 
			}else if(month==1)
			{
				weekBeginDay=day-day_of_week+31;
				weekEndDay=day+6-day_of_week;
				weekBeginMonth=month-1;
				weekEndMonth=month;
				weekBeginYear=year;
				weekEndYear=year;
				if(weekBeginMonth<=0)
				{
					weekBeginMonth=weekBeginMonth+12;
					weekBeginYear=weekBeginYear-1;
					if(weekBeginYear<0)
					{
						alert("错误!");
					}
				} 
			}else
			{	
				if(year%4==0&&year%400!=0 ||year%400==0)
				{
					weekBeginDay=day-day_of_week+29;
					weekEndDay=day+6-day_of_week;
					weekBeginMonth=month-1;
					weekEndMonth=month;
					weekBeginYear=year;
					weekEndYear=year;
				}else
				{
					weekBeginDay=day-day_of_week+28;
					weekEndDay=day+6-day_of_week;
					weekBeginMonth=month-1;
					weekEndMonth=month;
					weekBeginYear=year;
					weekEndYear=year;
				}
			}
		}else
		{
			if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
			{
				weekBeginDay=day-day_of_week;
				weekEndDay = day+6-day_of_week;
				weekBeginMonth=month;
				weekEndMonth=month;
				weekBeginYear=year;
				weekEndYear=year;
				if(weekEndDay>31)
				{
					weekEndDay=weekEndDay-31;
					weekEndMonth=month+1;
					if(weekEndMonth>12)
					{
						weekEndMonth=weekEndMonth-12;
						weekEndYear=year-1;
					}
					
				}
			}else if(month==4||month==6||month==9||month==11)
			{
				weekBeginDay=day-day_of_week;
				weekEndDay = day+6-day_of_week;
				weekBeginMonth=month;
				weekEndMonth=month;
				weekBeginYear=year;
				weekEndYear=year;
				if(weekEndDay>30)
				{
					weekEndDay=weekEndDay-30;
					weekEndMonth=month+1;
				}
			}else
			{
				if(year%4==0&&year%400!=0 ||year%400==0)
				{
					weekBeginDay=day-day_of_week;
					weekEndDay = day+6-day_of_week;
					weekBeginMonth=month;
					weekEndMonth=month;
					weekBeginYear=year;
					weekEndYear=year;
					if(weekEndDay>29)
					{
						weekEndDay=weekEndDay-29;
						weekEndMonth=month+1;
					}
				}else
				{
					weekBeginDay=day-day_of_week;
					weekEndDay = day+6-day_of_week;
					weekBeginMonth=month;
					weekEndMonth=month;
					weekBeginYear=year;
					weekEndYear=year;
					if(weekEndDay>28)
					{
						weekEndDay=weekEndDay-28;
						weekEndMonth=month+1;
					}
				}
			}
			
		}
		for(i=0;i<6;i++){
			if(weekEndDay>31&&(weekEndMonth==1||weekEndMonth==3||weekEndMonth==5||weekEndMonth==7||weekEndMonth==8||weekEndMonth==10||weekEndMonth==12)){
				weekEndMonth=weekEndMonth+1;
				weekEndDay = weekEndDay-31;
				if(weekEndMonth>12){
					weekEndMonth=weekEndMonth-12
					weekEndYear=weekEndYear+1;
				}
			}else if(weekEndDay>30&&(weekEndMonth==4||weekEndMonth==6||weekEndMonth==9||weekEndMonth==11)){
				weekEndMonth=weekEndMonth+1;
				weekEndDay=weekEndDay-30;
				if(weekEndMonth>12){
					weekEndMonth=weekEndMonth-12
					weekEndYear=weekEndYear+1;
				}
			}else if(weekEndDay>29&&weekEndMonth==2&&(weekBeginYear%4==0&&weekBeginYear%400!=0 ||weekBeginYear%400==0)){
				weekEndMonth=weekEndMonth+1;
				weekEndDay=weekEndDay-29;
				if(weekEndMonth>12){
					weekEndMonth=weekEndMonth-12
					weekEndYear=weekEndYear+1;
				}
			}else if(weekEndDay>28&&weekEndMonth==2){
				weekEndMonth=weekEndMonth+1;
				weekEndDay=weekEndDay-28;
				if(weekEndMonth>12){
					weekEndMonth=weekEndMonth-12
					weekEndYear=weekEndYear+1;
				}			
			}
			if(weekBeginDay>31&&(weekBeginMonth==1||weekBeginMonth==3||weekBeginMonth==5||weekBeginMonth==7||weekBeginMonth==8||weekBeginMonth==10||weekBeginMonth==12)){
				weekBeginDay = weekBeginDay - 31;
				weekBeginMonth = weekBeginMonth + 1;
				if(weekBeginMonth>12){
					weekBeginMonth=weekBeginMonth-12
					weekBeginYear=weekBeginYear+1;
				}
			}else if(weekBeginDay>30&&(weekBeginMonth==4||weekBeginMonth==6||weekBeginMonth==9||weekBeginMonth==11)){
				weekBeginDay = weekBeginDay - 30;
				weekBeginMonth = weekBeginMonth + 1;
				if(weekBeginMonth>12){
					weekBeginMonth=weekBeginMonth-12
					weekBeginYear=weekBeginYear+1;
				}
			}else if(weekBeginDay>29&&weekBeginMonth==2&&(weekBeginYear%4==0&&weekBeginYear%400!=0 ||weekBeginYear%400==0)){
				weekBeginDay = weekBeginDay - 29;
				weekBeginMonth = weekBeginMonth + 1;
				if(weekBeginMonth>12){
					weekBeginMonth=weekBeginMonth-12
					weekBeginYear=weekBeginYear+1;
				}
			}else if(weekBeginDay>28&&weekBeginMonth==2){
				weekBeginDay = weekBeginDay - 28;
				weekBeginMonth = weekBeginMonth + 1;
				if(weekBeginMonth>12){
					weekBeginMonth=weekBeginMonth-12
					weekBeginYear=weekBeginYear+1;
				}
			}
			/*if(i==3){
				alert(weekBeginYear+"!@#$%%"+back_begin_year+"!@#$%%"+weekEndYear+"!@#$%%"+back_end_year);
				alert(weekBeginMonth+"!@#$%%"+back_begin_month+"!@#$%%"+weekEndYear+"!@#$%%"+back_end_year);
				alert(weekBeginDay+"!@#$%%"+back_begin_day+"!@#$%%"+weekEndDay+"!@#$%%"+back_end_day);
			}*/
			if(weekBeginYear==_year&&weekEndYear==_year&&weekBeginMonth>(new Date().getMonth()+1)){
				break;
			}
			if(weekBeginYear==back_begin_year&&weekBeginMonth==back_begin_month&&weekBeginDay==back_begin_day&&weekEndYear==back_end_year&&weekEndMonth==back_end_month&&weekEndDay==back_end_day){
				str=str+"<td style='width: 80px;' class='td_poineter' onclick=' write_time("+"\""+weekBeginYear+"-"+weekBeginMonth+"-"+weekBeginDay+"@"+weekEndYear+"-"+weekEndMonth+"-"+weekEndDay+"\")'><b>"+weekBeginMonth+"."+weekBeginDay+"--"+weekEndMonth+"."+weekEndDay+"</b></td>";
			}else{
				str=str+"<td style='width: 80px;' class='td_poineter' onclick=' write_time("+"\""+weekBeginYear+"-"+weekBeginMonth+"-"+weekBeginDay+"@"+weekEndYear+"-"+weekEndMonth+"-"+weekEndDay+"\")'>"+weekBeginMonth+"."+weekBeginDay+"--"+weekEndMonth+"."+weekEndDay+"</td>";
			}
			weekBeginDay=weekEndDay+1;
			weekEndDay=weekEndDay+7;
			weekBeginMonth=weekEndMonth;
			weekBeginYear=weekEndYear;
		}
		str=str+"</tr></table>";
	document.getElementById("week_day").innerHTML =str;   
}

分享到:
评论

相关推荐

    js获取年月日和当月的天数

    ### JavaScript 获取年月日及当月...通过上述示例,我们可以清楚地了解到如何使用JavaScript来处理日期和时间,特别是在获取当前日期的年、月、日以及计算当月的总天数方面。这对于开发基于日期的应用程序非常有帮助。

    根据年月得到当月的天数及日期

    在编程领域,获取特定年月的天数是一项常见的任务,特别是在...如果你有一个名为"根据年月得到当月的天数及日期.txt"的文本文件,可能包含了这样的代码实现或更多详细说明,你可以阅读文件内容以获取更具体的实现细节。

    下拉列表关于年月日的显示

    它首先定义了一个数组`MonHead`来存储每个月的标准天数,然后获取当前年份,并且根据当前年份动态填充年份和月份的下拉列表。具体步骤如下: - 获取当前年份并将其存储在变量`y`中。 - 使用`for`循环来填充年份下拉...

    根据年月获取这个月份的所有日期

    "根据年月获取这个月份的所有日期"这个任务可以通过编写一个函数来实现,就像提供的代码片段所示。下面将详细解释这个函数的工作原理及其涉及到的JavaScript日期对象和循环结构的知识点。 首先,我们来看一下`...

    html年月日三下拉框联动

    2. **JavaScript事件处理**:通过`onchange`事件,我们可以检测到下拉框中选项的改变,并执行相应的JavaScript函数来更新其他下拉框的内容。 3. **JavaScript日期对象**:JavaScript中的`Date`对象提供了操作日期和...

    JavaScript时间操作之年月日星期级联操作

    标题和描述中提到的“JavaScript时间操作之年月日星期级联操作”指的是在Web页面上利用JavaScript编程语言实现一个级联的日期选择器功能,此功能允许用户从下拉列表中选择年、月、日,并且能够根据选择自动更新下一...

    JS如何判断某天为星期几

    函数中使用了一个字符串`keystr`,这个字符串包含了每个月相对于1月的天数差,但是这个方法并不是标准的或广为人知的算法,它依赖于一个特定的基线年份(2000年),并且通过一系列复杂的数学运算来得出结果。具体来...

    JS根据年月获得当月天数的实现代码

    这里使用了一个巧妙的方法来获取某月的最后一天,即将日期参数设置为0,这在JavaScript中表示上一个月的最后一天。而`Date`对象会自动计算并转换成当月最后一天的日期。例如,如果输入的月份是3,那么`new Date(year...

    小程序实现横向滑动日历效果

    本示例中,我们将详细探讨如何在小程序中创建这样一个功能。 首先,我们需要一个`scroll-view`组件来实现横向滚动的效果。在HTML部分,可以看到`&lt;scroll-view&gt;`标签,设置了`scroll-x`属性,这表明我们希望在水平...

    jquery年月日插件

    如果需要对插件进行进一步的定制,比如更改日期显示格式或添加特定的事件处理,可以通过扩展 jQuery 对象来实现。例如,添加一个点击事件监听器,获取用户选择的日期: ```javascript $("#year, #month, #day").on...

    根据当前年月得到日(天),每行显示7列,有效果图

    如果每行显示7列,那么在构建表格时,每个日期都要放在一个`&lt;td&gt;`中,并且确保每7个日期后换行,即开始一个新的`&lt;tr&gt;`。如果需要每行显示的列数可变,可以通过参数控制`&lt;tr&gt;`的循环次数和每次循环中`&lt;td&gt;`的数量。 ...

    下拉框年月日3级联动

    这表明实现这一功能可能依赖于jQuery,一个广泛使用的JavaScript库,它简化了DOM操作和事件处理。由于文件名中提到了"无法初始化值",这可能意味着在某些情况下,比如页面加载或动态添加元素时,初始日期可能无法...

    JavaScript实现简单日历效果

    然后通过创建一个Date对象来获取该月第一天是星期几,以及该月的天数。接着,清除表格中的旧数据,根据计算结果循环向表格中添加天数,从而生成日历。 2. initial()函数:这个函数用于初始化select标签中的option...

    下拉框选择年月日代码

    ### 下拉框选择年月日代码解析 #### 一、概述 本文将详细介绍一个用于实现年、月、日三联动下拉框的HTML与JavaScript代码。该代码通过动态生成年份、月份及日期的下拉选项,并根据所选年份和月份自动调整日期范围...

    年月日、时分秒控件的实现

    "年月日时分秒控件的实现"是一个常见的任务,它涉及到了用户界面(UI)设计和交互逻辑的编写。下面,我们将详细探讨这个主题,包括如何创建这样一个自定义控件,以及如何实现上下滑动设定时间的功能。 1. **自定义...

    denglu.rar

    总的来说,这个“微信小程序签到工具类”是一份实用性极强的学习资源,无论你是初学者还是有一定经验的开发者,都能从中受益。通过实际操作和学习,你可以掌握微信小程序开发的基本技巧,进一步提升你的编程技能。在...

    js根据给定的日期计算当月有多少天实现思路及代码

    此实例使用了年月日三个参数来创建一个日期对象,但创建的是当月的第一天。然后通过Date对象的`getDate()`方法获取这个日期对象的天数,由于已经确定为第一天,直接输出的就是当月的天数。 这样的实现方法是有效的...

    Vue日历组件,周和月的显示模式

    在本文中,我们将深入探讨如何使用Vue.js创建一个具有周和月切换显示模式的日历组件。Vue.js是一款轻量级的前端JavaScript框架,以其简洁的API和强大的功能深受开发者喜爱。构建日历组件是常见的需求,它能帮助用户...

Global site tag (gtag.js) - Google Analytics