断点

每天进步一点点!
posts - 174, comments - 56, trackbacks - 0, articles - 21

JS实现下一天的显示

Posted on 2010-01-14 21:28 断点 阅读(528) 评论(0)  编辑  收藏 所属分类: JScript

1、(背景:以前在做系统查询的时候,要显示系统当天时间以及下一天,当前时间很容易写出,可下一天不会写,下面是自己试着写的一个。)
function tomorrow(){
     var date = new Date();
     var year = date.getYear(); //取得当前年份命令
     var month = date.getMonth() 1;
     var day = date.getDate();
     var dateStr=null;
     if(year%4==0 && year%100!=0 || year%400==0){ //为闰年
      if(month==1||month==3||month==5||month==7||month==8||month==10){
       if(day==31){
        month = month 1;
        if(month < 10){ month ='0' month ; }
        dateStr = year "-" month "-" "01" ;
       }else if(day!=31){
        day = day 1;
        if(month < 10){ month ='0' month ; }
        if(day < 10){ day ='0' day ; }
        dateStr = year "-" month "-" day ;}
      }else if(month==4||month==6||month==9||month==11){
       if(day==30){
        month=month 1;
        if(month < 10){ month ='0' month ; }
        dateStr = year "-" month "-" "01" ;
       }else if(day!=30){
        day = day 1;
        if(month < 10){ month ='0' month ; }
        if(day < 10){ day ='0' day ; }
        dateStr = year "-" month "-" day ;}
      }else if(month==12){
       if(day==31){month="01";dateStr = year 1 "-" month "-" "01" ;}
       else if(day!=31){
        day = day 1;
        if(month < 10){ month ='0' month ; }
        if(day < 10){ day ='0' day ; }
        dateStr = year "-" month "-" day ;}
      }else if(month==2){
       if(day==29){month="03";dateStr = year "-" month "-" "01" ;}
       else if(day!=29){
        day = day 1;
        if(month < 10){ month ='0' month ; }
        if(day < 10){ day ='0' day ; }
        dateStr = year "-" month "-" day ;}
      }
     }else { //非闰年
      if(month==1||month==3||month==5||month==7||month==8||month==10){
       if(day==31){
        month=month 1;
        if(month < 10){ month ='0' month ; }
        dateStr = year "-" month "-" "01" ;
       }else if(day!=31){
        day = day 1;
        if(month < 10){ month ='0' month ; }
        if(day < 10){ day ='0' day ; }
        dateStr = year "-" month "-" day ;}
      }else if(month==4||month==6||month==9||month==11){
       if(day==30){
        month=month 1;
        if(month < 10){ month ='0' month ; }
        dateStr = year "-" month "-" "01" ;
       }else if(day!=30){
        day = day 1;
        if(month < 10){ month ='0' month ; }
        if(day < 10){ day ='0' day ; }
        dateStr = year "-" month "-" day ;}
      }else if(month==12){
       if(day==31){month="01"; dateStr = year 1 "-" month "-" "01" ;}
       else if(day!=31){
        day = day 1;
        if(month < 10){ month ='0' month ; }
        if(day < 10){ day ='0' day ; }
        dateStr = year "-" month "-" day ;}
      }else if(month==2){
       if(day==28){month="03";dateStr = year "-" month "-" "01" ;}
       else if(day!=28){
        day = day 1;
        if(month < 10){ month ='0' month ; }
        if(day < 10){ day ='0' day ; }
        dateStr = year "-" month "-" day ;}
      }
       }
     alert(dateStr); //为了方便查看结果才加上去的
}

2、自己写的特容易出错,调用js自带的函数,使产生的对象自加一,很好的控制了2008-10-32、undefined 、null的产生。
function nextdate(){
var date = new Date();
var b = date.getDate();
b += 1;
date.setDate(b);
var year = date.getYear(); //取得当前年份命令
var month = date.getMonth()+1;
var day = date.getDate();
if(month < 10){ month ='0'+ month ; }
if(day < 10){ day ='0'+ day ; }
var dateStr = year+ "-"+ month+"-"+day ;
alert(dateStr);


posted @ 2009-05-09 17:24 断点 阅读(38) | 评论 (0)

只有注册用户登录后才能发表评论。


网站导航: