Js计算指定日期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function   DateAdd(interval,number,date) 
/*
  *   功能:实现VBScript的DateAdd功能.
  *   参数:interval,字符串表达式,表示要添加的时间间隔.
  *   参数:number,数值表达式,表示要添加的时间间隔的个数.
  *   参数:date,时间对象.
  *   返回:新的时间对象.
  *   var   now   =   new   Date();
  *   var   newDate   =   DateAdd( "d ",5,now);
  *---------------   DateAdd(interval,number,date)   -----------------
  */ 
        switch(interval) 
        
                case   "y "   :   { 
                        date.setFullYear(date.getFullYear()+number); 
                        return   date; 
                        break
                
                case   "q "   :   { 
                        date.setMonth(date.getMonth()+number*3); 
                        return   date; 
                        break
                
                case   "m "   :   { 
                        date.setMonth(date.getMonth()+number); 
                        return   date; 
                        break
                
                case   "w "   :   { 
                        date.setDate(date.getDate()+number*7); 
                        return   date; 
                        break
                
                case   "d "   :   { 
                        date.setDate(date.getDate()+number); 
                        return   date; 
                        break
                
                case   "h "   :   { 
                        date.setHours(date.getHours()+number); 
                        return   date; 
                        break
                
                case   "m "   :   { 
                        date.setMinutes(date.getMinutes()+number); 
                        return   date; 
                        break
                
                case   "s "   :   { 
                        date.setSeconds(date.getSeconds()+number); 
                        return   date; 
                        break
                
                default   :   { 
                        date.setDate(d.getDate()+number); 
                        return   date; 
                        break
                
        
    
var   now   =   new   Date(); 
//加五天. 
var   newDate   =   DateAdd( "d ",5,now); 
alert(newDate.toLocaleDateString()) 
//加两个月. 
newDate   =   DateAdd( "m ",2,now); 
alert(newDate.toLocaleDateString()) 
//加一年 
newDate   =   DateAdd( "y ",1,now); 
alert(newDate.toLocaleDateString())

  

posted @   rianbowymail  阅读(104)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示