首页 新闻 会员 周边

求助,用js实现日期加减天数,求实现方法?

0
悬赏园豆:10 [已解决问题] 解决于 2012-02-17 09:21

用js实现日期加减天数,,根据当前的日期,按旁边图片实现天数的增减,求指教。。。。。。。

虎犊求知的主页 虎犊求知 | 初学一级 | 园豆:122
提问于:2012-02-08 10:45
< >
分享
最佳答案
0
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
var count=0;
function load(){
GetDate();
}
function prev(){
count
--;
GetDate(count);
}
function next(){
count
++;
GetDate(count);
}

function GetDate(val){
var date=new Date();
if(val!=undefined){
var currDate=date.getDate();
date
=new Date(date.setDate(currDate+val));
}
document.getElementById(
"time").innerText=(date.getMonth()+1)+"-"+date.getDate()+"/"+GetCnWeek(date.getDay());
}

function GetCnWeek(number){
switch(number){
case0:
return"周日";
case1:
return"周一";
case2:
return"周二";
case3:
return"周三";
case4:
return"周四";
case5:
return"周五";
case6:
return"周六";
}
}
</script>
</head>
<body onload="load()">
<div id="time"></div>
<input type="button" onclick="prev()" value="<"/>&nbsp;
<input type="button" onclick="next()" value=">">
</body>
</html>

样式你自己调整下就OK了

在线演示:http://jscode.chinacxy.com/code/bdf7f4a30386ab58cb5b8a8ebe78fb24.aspx

收获园豆:10
artwl | 专家六级 |园豆:16736 | 2012-02-08 11:17

在线演示用的这个网站挺不错的。

LCM | 园豆:6876 (大侠五级) | 2012-02-08 11:40

@LCM: 什么网站呢

虎犊求知 | 园豆:122 (初学一级) | 2012-02-08 12:59

@虎犊求知: 

你要的是这个效果吗?

artwl | 园豆:16736 (专家六级) | 2012-02-08 13:00

你好,怎么这代码没出效果来得,是怎么回事呢??

 

虎犊求知 | 园豆:122 (初学一级) | 2012-02-08 13:08

@虎犊求知: 

你把我回复中的代码保存成一个html页面,打开后没效果???

或打开这个页面可以在线看:http://jscode.chinacxy.com/code/bdf7f4a30386ab58cb5b8a8ebe78fb24.aspx

artwl | 园豆:16736 (专家六级) | 2012-02-08 13:09

@artwl: 大侠,请问有没学习js的入门书吗,求介绍????

虎犊求知 | 园豆:122 (初学一级) | 2012-02-08 13:28

@虎犊求知: 

推荐《JavaScript高级程序设计》人民邮电出版社(曹力 张欣 等译)

artwl | 园豆:16736 (专家六级) | 2012-02-08 13:32
其他回答(1)
0

你看下这样可以么

<script type="text/javascript">
var d = new Date();
function addDate(days){
d.setDate(d.getDate()+days);
var m=d.getMonth()+1;
document.getElementById("msg").value = d.getFullYear()+'-'+m+'-'+d.getDate();
}

</script>
<input type="text" id="msg"/>
<input type="button" value="<" onclick="addDate(-1)"/>
<input type="button" value=">" onclick="addDate(1)"/>

日期要显示的格式,你可以参考下这个

http://www.cnblogs.com/carekee/articles/1678041.html

LCM | 园豆:6876 (大侠五级) | 2012-02-08 11:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册