
正文
javascript期,JavaScript期末作品
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
javascript 日期 加减
用Javascript实现(经测试答案正确):
html
head
meta http-equiv="Content-Type" content="text/html; charset=uft-8"
script language="javascript"
function window_onload()
{
var LSTR_Date = "20090115";
//(日期表示只有这一种格式,不支持民国年)
var LSTR_AddDays =45;
var LSTR_DateType = "YYYYMMDD";
alert(AddDate(LSTR_Date,LSTR_AddDays,LSTR_DateType));
}
function AddDate(LISTR_Date,LISTR_AddDays,LISTR_DateType)
{
var LSTR_YY=0;
var LSTR_MM=0;
var LSTR_DD=0;
var LINT_FLAG=0;
//检查日期格式为 "YYYYMMDD" 或
//"MMDDYYYY" 且长度为8码
if((LISTR_DateType!="YYYYMMDD") (LISTR_DateType!="MMDDYYYY") (LISTR_Date.length!=8))
return false;
if(LISTR_DateType=="MMDDYYYY")
LISTR_Date=LISTR_Date.substr(4,4)+LISTR_Date.substr(0,4);
LSTR_YY=parseInt(LISTR_Date.substr(0,4),10);
LSTR_MM=parseInt(LISTR_Date.substr(4,2),10);
LSTR_DD=parseInt(LISTR_Date.substr(6,2),10)+parseInt(LISTR_AddDays,10);
while(LINT_FLAG==0)
{
switch (LSTR_MM)
{
case 2:
if ((LSTR_YY % 4) != 0)
{
if (LSTR_DD 28)
{
LSTR_DD -=28;
LSTR_MM =3;
}
else
{
LINT_FLAG=1;
}
}
else
{
if (((LSTR_YY % 100) == 0) ((LSTR_YY % 400) != 0))
{
if (LSTR_DD 28)
{
LSTR_DD -=28;
LSTR_MM =3;
}
else
{
LINT_FLAG=1;
}
}
else
{
if (LSTR_DD 29)
{
LSTR_DD -=29;
LSTR_MM =3;
}
else
{
LINT_FLAG=1;
}
}
}
break;
case 4:
case 6:
case 9:
case 11:
if (LSTR_DD 30)
{
LSTR_DD -=30;
LSTR_MM +=1;
}
else{LINT_FLAG=1;}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if (LSTR_DD 31)
{
LSTR_DD -=31;
LSTR_MM +=1;
}
else
{
LINT_FLAG=1;
}
break;
case 12:
if (LSTR_DD 31)
{
LSTR_DD -=31;
LSTR_MM=1;
LSTR_YY +=1;
}
else
{
LINT_FLAG=1;
}
break;
default:
return;
break;
}
}
if (LSTR_MM10)
{
LSTR_MM="0" +LSTR_MM;
}
if (LSTR_DD10)
{
LSTR_DD="0" +LSTR_DD;
}
if(LISTR_DateType=="MMDDYYYY")
return LSTR_MM+""+LSTR_DD+""+LSTR_YY+"";
else
return LSTR_YY+""+LSTR_MM+""+LSTR_DD+"";
}
/script
/head
body onload="window_onload();"
/body
/html
相关问答
Q1: javascript 日期计算
你这个问题太费劲了,不过终于写好了
===================================================
script language = "javascript"
/**
* 跟据年份和月份返回当前日期的最大天数
*/
function getMonthMaxDay(year, month) {
if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else if (month != 2) {
return 31;
} else {
if (year % 4 == 0 || (year % 100 == 0 year % 400 == 0)) {
if (month == 2) {
return 29;
}
} else {
if (month == 2) {
return 28;
}
}
}
return 0;
}
function getNextNumDay(nowDate, dayNum){
var intBeginYear = parseInt(nowDate.substring(0, 4),10);
//从"-"后截取月数
var intBeginMonth = parseInt(nowDate.substring(nowDate.indexOf("-") + 1, nowDate.indexOf("-") + 3),10);
var intBeginDate = parseInt(nowDate.substring(nowDate.lastIndexOf("-") + 1, nowDate.lastIndexOf("-") + 3),10);
var day = new Date(Date.parse(nowDate.replace(/-/g, '/'))); //格式化时间
var week = day.getDay();//获得今天是周几
var restDay = parseInt(dayNum/7,10)*2 + dayNum;
var otherDay = dayNum%7;
if(week == 5)//周5加两天
restDay+=2;
if(week == 6)//周6加一天
restDay+=1;
var nowDay1 = getAfterDay(dayNum,intBeginDate,intBeginMonth,intBeginYear);
var newday = new Date(Date.parse(nowDay1.replace(/-/g, '/'))); //格式化时间
var newWeek = newday.getDay();
if(week == 5)
restDay+=2;
if(week == 6)
restDay+=1;
return getAfterDay(restDay,intBeginDate,intBeginMonth,intBeginYear);
}
function getAfterDay(dayNum,intBeginDate,intBeginMonth,intBeginYear){
date2 = intBeginDate + dayNum;
year2 = intBeginYear;
month2 = intBeginMonth;
maxDate2 = getMonthMaxDay(year2,month2);
if(date2 maxDate2){
date2 = date2 - maxDate2;
month2 += 1;
if(month2 12){
month2 = month2 - 12;
year2 += 1;
}else if(month2 == 12){
month2 = 1;
year2 += 1;
}
}else if(date2 == maxDate2){
date2 = maxDate2;
}
if(date2 getMonthMaxDay(year2,month2)){
getAfterDay(date2,1,month2,year2)//如果减去日期后还是大于下月的天数则递归调用
}
if(parseInt(date2,10) 10){
date2 = '0' + parseInt(date2,10);
}
if(parseInt(month2,10) 10){
month2 = '0' + parseInt(month2,10);
}
var resultDate2 = year2+"-"+month2+"-"+date2;
return resultDate2;
}
alert(getNextNumDay('2011-01-22',9));
/script
Q2: javascript 日期
定义个y,y的值是t除以years,t的值是1970.1.1到现在的毫秒数,years值是1年多少毫秒,所以y的值就是1970年到现在多少年
Q3: 关于javascript全局变量的有效期
JScript 在运行代码前处理变量声明,所以声明是位于一个条件块中还是其他某些结构中无关紧要。JScript 找到所有的变量后立即运行函数中的代码。如果变量是在函数中显式声明的 — 也就是说,如果它出现于赋值表达式的左边但没有用 var 声明 — 那么将把它创建为全局变量。
JS既然是嵌入在网页中的,那么它的全局变量当然和网页的生命周期一样,内存也是由浏览器来分配和收回,只要不关闭网页就会一直存在啊
Q4: 关于javascript日期函数问题
function dateTest(firstDate) {
var fDate = firstDate.split("-");
firstDate = new Date();
var sss = "fffff:" + fDate[0] + "-" + fDate[1] + "-" + fDate[2];
alert(sss);
var fDateMonth = parseFloat(fDate[1]) - 1;
alert(fDateMonth);
firstDate.setYear(parseFloat(fDate[0]));
firstDate.setMonth(fDateMonth);
firstDate.setDate(1);
alert(firstDate);
firstDate.setHours(0);
firstDate.setMinutes(0);
firstDate.setSeconds(0);
firstDate.setMilliseconds(0);
alert(firstDate);
}
这样试试






