在网页中有两个文本框,第一个输入日期(如:2008-8-8-0900,其它时间格式也行),第二个输入小时(如:3小时1分),然后在网页中任何地方出现2008-8-18-1101,上面的意思理解吧?此问题好像有点难度?
解决方案 »
- javascript 如何判断当前网站是否属于安全站点?
- js中是不是可以省略getElementById,下面的代码有没有注释部分都照样成功运行啊
- overflow:hidden 感觉没有多少用啊,谈谈他的存在性
- 怎样单击客户端Img图像控件时调用服务器里的函数并把它的ID作参数传递给该函数?
- 请问以下代码为何firefox不执行
- 高手们进来帮我看看问题好吗?onSumbit调用JavaScript的问题
- IE7对window.open()中windowname的支持
- json出现不明字符的问题
- 我看了好多帖子都是说的javascript如何取得浏览器的语言属性,我的问题是如何通过javascript设置浏览器的语言?
- 判断时间大小
- 请教问题:使用goAhead web service 想做网页无操作20分钟后断开,该怎样实现呢? 谢谢,我是初学者,希望各位多多关照!
- 超级链接获取地址(有难度啊)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="[email protected]" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
<input type="text" id="txtStartTime" value="2008-8-8-0900" /><br />
<input type="text" id="txtInterval" value="3小时1分" /><br />
<input type="button" id="btnCalculate" value="Calculate" /><br />
<span style="color: red;" id="spnEndTime"></span>
</body>
<script type="text/javascript">
<!--
function $(sId)
{
return document.getElementById(sId);
}$("btnCalculate").onclick = function()
{
var aDate = $("txtStartTime").value.split("-");
// 月份是从 0 到 11 之间的整数( 1 月至 12 月)。因此减 1。
var dStart = new Date(aDate[0], (parseInt(aDate[1])-1), aDate[2], aDate[3].substr(0,2), aDate[3].substr(2,2));
// For debug only.
//alert(dStart.toLocaleString()); var aInterval = $("txtInterval").value.split("小时");
aInterval[0] = parseInt(aInterval[0]);
aInterval[1] = parseInt(aInterval[1]); var iInterval = (60*aInterval[0] + aInterval[1]) * 60 * 1000;
var dEnd = new Date(dStart.valueOf() + iInterval);
// For debug only.
//alert(dEnd.toLocaleString()); var sEnd = dEnd.getFullYear() + "-";
sEnd += (dEnd.getMonth() + 1) + "-";
sEnd += dEnd.getDate() + "-";
sEnd += dEnd.getHours()>10 ? dEnd.getHours() : ("0"+dEnd.getHours());
sEnd += dEnd.getMinutes()>10 ? dEnd.getMinutes() : ("0"+dEnd.getMinutes());
$("spnEndTime").innerHTML = sEnd;
};//-->
</script>
</html>
http://download.csdn.net/source/308913样式表中文手册
http://download.csdn.net/source/304124JScript语言参考
http://download.csdn.net/source/308916
function printLine(s){
document.write(s,"<br />");
}
function timeParse(timeString,pattern){
var date=new Date();
var match=timeString.match(pattern);
date.setYear(parseInt(match[1],10));
date.setMonth(parseInt(match[2],10)-1);
date.setDate(parseInt(match[3],10));
date.setHours(parseInt(match[4],10));
date.setMinutes(parseInt(match[5],10));
date.setSeconds(0);
return date;
}
Date.prototype.addTime=function(timeSpan){
this.setYear(this.getYear()+timeSpan.years);
this.setMonth(this.getMonth()+timeSpan.months);
this.setDate(this.getDate()+timeSpan.days);
this.setHours(this.getHours()+timeSpan.hours);
this.setMinutes(this.getMinutes()+timeSpan.minutes);
}
Date.prototype.toString=function(){
return this.getYear()+"-"
+(this.getMonth()+1)+"-"
+this.getDate()+" "
+this.getHours()+":"
+this.getMinutes();
}
function timeSpanParse(timeSpanString,pattern){
var match=timeSpanString.match(pattern);
var y=match[1]==""?0:parseInt(match[1]);
var mon=match[2]==""?0:parseInt(match[2]);
var d=match[3]==""?0:parseInt(match[3]);
var h=match[4]==""?0:parseInt(match[4]);
var m=match[5]==""?0:parseInt(match[5]);
return {years:y,months:mon,days:d,hours:h,minutes:m};
}
var reg_time=/^(\d{1,4})\-(\d{1,2})\-(\d{1,2}) (\d{1,2}):(\d{1,2})$/;
var date=timeParse("2008-8-8 09:00",reg_time);
printLine(date);
var reg_timeSpan=/^(?:(\d+)年)?(?:(\d+)个?月)?(?:(\d+)天)?(?:(\d+)小时)?(?:(\d+)分钟?)?$/;
var timeSpan=timeSpanParse("10分",reg_timeSpan);
date.addTime(timeSpan);
printLine("增加10分钟后:"+date);
timeSpan=timeSpanParse("3小时10分",reg_timeSpan);
date.addTime(timeSpan);
printLine("增加3小时10分后:"+date);
timeSpan=timeSpanParse("2天3小时10分",reg_timeSpan);
date.addTime(timeSpan);
printLine("增加2天1小时5分后:"+date);
timeSpan=timeSpanParse("1个月1天1小时1分",reg_timeSpan);
date.addTime(timeSpan);
printLine("增加1个月1天1小时1分后:"+date);timeSpan=timeSpanParse("2年1小时1分钟",reg_timeSpan);
date.addTime(timeSpan);
printLine("增加2年1小时1分钟后:"+date);
</script>
输出:
-------------
2008-8-8 9:0
增加10分钟后:2008-8-8 9:10
增加3小时10分后:2008-8-8 12:20
增加2天1小时5分后:2008-8-10 15:30
增加1个月1天1小时1分后:2008-9-11 16:31
增加2年1小时1分钟后:2010-9-11 17:32