js实现输入日期输出后一天.
这是我们软件测试书上的一道习题.老师让我们给用代码实现之后再测试bug
做一份测试报告.
实现方式: c++ c# java jsp js ...........
我用的js
各位看官可否 帮我测测bug
测试网址(www.jichangxiu.cn)
如果留网页犯规.请管理员大哥(大姐)删帖,拜谢....
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>计算年月日</title>
<style>
#form div{
font-size: 8px;
height: 10px;
color: red;
}
#contanel{
width: 400px;
height: 250px;
padding: 10px;
background-color: antiquewhite;
text-align: center;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
</style>
<script>
function subchange(str){
var year = document.getElementById("year");
var month = document.getElementById("month");
var day = document.getElementById("day");
var showyear = document.getElementById("showyear");
var showmonth = document.getElementById("showmonth");
var showday = document.getElementById("showday");
if(str == "yearfocus"){
showyear.innerHTML="请输入年份为1800-2050中的任意一年";
}else if(str == "yearblur"){
showyear.innerHTML="";
}else if(str == "monthfocus"){
showmonth.innerHTML="请输入月份为1-12中的任意月份";
}else if(str == "monthblur"){
showmonth.innerHTML="";
}if(str == "dayfocus"){
showday.innerHTML="请输入天数为1-31中的任意天数";
}else if(str == "dayblur"){
showday.innerHTML="";
}
}
function subclick(){
var year = Number(document.getElementById("year").value);
var month = Number(document.getElementById("month").value);
var day = Number(document.getElementById("day").value);
var showyear = document.getElementById("showyear");
var showmonth = document.getElementById("showmonth");
var showday = document.getElementById("showday");
if(year == '' || isNaN(year) || year < 1800 || year >2050){
showyear.innerHTML="输入不能为空 or 年份只可以输入数字类型 or 年份输入错误";
}else if(month == '' || isNaN(month) || month < 1 || month > 12){
showmonth.innerHTML="输入不能为空 or 月份只可以输入数字类型 or 月份输入错误";
}else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
if(day == '' || isNaN(day) || day < 1 || day > 31){
showday.innerHTML="输入不能为空 or 天数只可以输入数字类型 or 天数输入错误";
return;
}
}else if(month == 4 || month == 6 || month == 9 || month == 11){
if(day == '' || isNaN(day) || day < 1 || day > 30){
showday.innerHTML="输入不能为空 or 天数只可以输入数字类型 or 天数输入错误";
return;
}
}else if(month == 2){
if((year % 4==0 && year % 100 != 0) || year % 400 == 0){
if(day == '' || isNaN(day) || day < 1 || day > 29){
showday.innerHTML="输入不能为空 or 天数只可以输入数字类型 or 天数输入错误";
return;
}
}else{
if(day == '' || isNaN(day) || day < 1 || day > 28){
showday.innerHTML="输入不能为空 or 天数只可以输入数字类型 or 天数输入错误";
return;
}
}
}
setdate(year , month , day);
}
function setdate(year , month , day){
var result = document.getElementById("result");
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
if(day == 31){
if(month == 12){
year += 1 ;
month = 1 ;
day = 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}else{
month += 1 ;
day = 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}
}else{
day += 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}
}else if(month == 4 || month == 6 || month == 9 || month == 11){
if(day == 30){
if(month == 12){
year += 1 ;
month = 1 ;
day = 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}else{
month += 1 ;
day = 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}
}else{
day += 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}
}else if(month == 2){
if((year % 4==0 && year % 100 != 0) || year % 400 == 0){
if(day == 29){
if(month == 12){
year += 1 ;
month = 1 ;
day = 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}else{
month += 1 ;
day = 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}
}else{
day += 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}
}else{
if(day == 28){
if(month == 12){
year += 1 ;
month = 1 ;
day = 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}else{
month += 1 ;
day = 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}
}else{
day += 1 ;
result.innerHTML=year + "-" + month + "-" + day;
}
}
}
}
</script>
</head>
<body>
<div id="contanel">
<form action="#" method="get" id="form">
年:<input type="text" id="year"/>
<div id="showyear" class="rigth">
</div>
<br />
月:<input type="text" id="month"/>
<div id="showmonth">
</div>
<br />
日:<input type="text" id="day"/>
<div id="showday">
</div>
<br />
<input type="button" value="提交"/>
</form>
<br />
结果为:<br />
<div id="result"></div>
</div>
</body>
</html>
从入门到大佬到跑路哈哈哈哈 只可以输入数字类型 那一长串的if else 可以使用switch来稍微优化一下
页:
[1]