The ramblings of web developer Beau Brownlee

 
June 20th, 2008

There are many date add methods online. I recently needed a simple bulletproof method for dateadd and didn’t like any of the ones i found online so i figured i would actually sit down and write one ;) . Here’s what I came up with.

The Code

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function DateAdd(objDate, strInterval, intIncrement)
    {
        if(typeof(objDate) == "string")
        {
            objDate = new Date(objDate);
 
            if (isNaN(objDate))
            {
                throw("DateAdd: Date is not a valid date");
            }
        }
        else if(typeof(objDate) != "object" || objDate.constructor.toString().indexOf("Date()") == -1)
        {
            throw("DateAdd: First parameter must be a date object");
        }
 
        if(
        strInterval != "M"
        && strInterval != "D"
        && strInterval != "Y"
        && strInterval != "h"
        && strInterval != "m"
        && strInterval != "uM"
        && strInterval != "uD"
        && strInterval != "uY"
        && strInterval != "uh"
        && strInterval != "um"
        && strInterval != "us"
        )
        {
            throw("DateAdd: Second parameter must be M, D, Y, h, m, uM, uD, uY, uh, um or us");
        }
 
        if(typeof(intIncrement) != "number")
        {
            throw("DateAdd: Third parameter must be a number");
        }
 
        switch(strInterval)
        {
            case "M":
            objDate.setMonth(parseInt(objDate.getMonth()) + parseInt(intIncrement));
            break;
 
            case "D":
            objDate.setDate(parseInt(objDate.getDate()) + parseInt(intIncrement));
            break;
 
            case "Y":
            objDate.setYear(parseInt(objDate.getYear()) + parseInt(intIncrement));
            break;
 
            case "h":
            objDate.setHours(parseInt(objDate.getHours()) + parseInt(intIncrement));
            break;
 
            case "m":
            objDate.setMinutes(parseInt(objDate.getMinutes()) + parseInt(intIncrement));
            break;
 
            case "s":
            objDate.setSeconds(parseInt(objDate.getSeconds()) + parseInt(intIncrement));
            break;
 
            case "uM":
            objDate.setUTCMonth(parseInt(objDate.getUTCMonth()) + parseInt(intIncrement));
            break;
 
            case "uD":
            objDate.setUTCDate(parseInt(objDate.getUTCDate()) + parseInt(intIncrement));
            break;
 
            case "uY":
            objDate.setUTCFullYear(parseInt(objDate.getUTCFullYear()) + parseInt(intIncrement));
            break;
 
            case "uh":
            objDate.setUTCHours(parseInt(objDate.getUTCHours()) + parseInt(intIncrement));
            break;
 
            case "um":
            objDate.setUTCMinutes(parseInt(objDate.getUTCMinutes()) + parseInt(intIncrement));
            break;
 
            case "us":
            objDate.setUTCSeconds(parseInt(objDate.getUTCSeconds()) + parseInt(intIncrement));
            break;
        }
        return objDate;
    }

The first argument (objDate) can be a string or a javascript Date object and is tested/converted at line 3. Throughout the function I throw exceptions because I needed a strict function that would stop everything if it couldn’t return a date. This could be changed to returning a default date or something like that, but for the purposes of this function you will need to have a try catch around the function call to handle the custom error. The second argument (strInterval) is what interval the function will increment/decrement and takes string values (self explanatory). The third is the number the date will increment/decrement.

Usage

Using this method is simple to use. Here’s a quick example:

1
2
3
4
5
6
7
8
9
// String date method
        var mydate = '6/20/2008';
        mydate = DateAdd(mydate, "D", -10);
        alert("The date is now: " + mydate.getDate());
 
        // Date object method
        var mydate = new Date();
        mydate = DateAdd(mydate, "D", 5);
        alert("The date is now: " + mydate.getDate());

Modifications

I originally wrote this function to receive either date objects or string methods, but we could pass a valid string into a date and prototype the javascript Date object like so:

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
73
74
75
76
Date.prototype.Add = function(strInterval, intIncrement)
    {
        if(
        strInterval != "M"
        && strInterval != "D"
        && strInterval != "Y"
        && strInterval != "h"
        && strInterval != "m"
        && strInterval != "uM"
        && strInterval != "uD"
        && strInterval != "uY"
        && strInterval != "uh"
        && strInterval != "um"
        && strInterval != "us"
        )
        {
            throw("DateAdd: Second parameter must be M, D, Y, h, m, uM, uD, uY, uh, um or us");
        }
 
        if(typeof(intIncrement) != "number")
        {
            throw("DateAdd: Third parameter must be a number");
        }
 
        switch(strInterval)
        {
            case "M":
            this.setMonth(parseInt(this.getMonth()) + parseInt(intIncrement));
            break;
 
            case "D":
            this.setDate(parseInt(this.getDate()) + parseInt(intIncrement));
            break;
 
            case "Y":
            this.setYear(parseInt(this.getYear()) + parseInt(intIncrement));
            break;
 
            case "h":
            this.setHours(parseInt(this.getHours()) + parseInt(intIncrement));
            break;
 
            case "m":
            this.setMinutes(parseInt(this.getMinutes()) + parseInt(intIncrement));
            break;
 
            case "s":
            this.setSeconds(parseInt(this.getSeconds()) + parseInt(intIncrement));
            break;
 
            case "uM":
            this.setUTCMonth(parseInt(this.getUTCMonth()) + parseInt(intIncrement));
            break;
 
            case "uD":
            this.setUTCDate(parseInt(this.getUTCDate()) + parseInt(intIncrement));
            break;
 
            case "uY":
            this.setUTCFullYear(parseInt(this.getUTCFullYear()) + parseInt(intIncrement));
            break;
 
            case "uh":
            this.setUTCHours(parseInt(this.getUTCHours()) + parseInt(intIncrement));
            break;
 
            case "um":
            this.setUTCMinutes(parseInt(this.getUTCMinutes()) + parseInt(intIncrement));
            break;
 
            case "us":
            this.setUTCSeconds(parseInt(this.getUTCSeconds()) + parseInt(intIncrement));
            break;
        }
        return this;
    }

By simply changing the code on line 1 to prototype the Date object we can now easily use this method in any date as though it were a method of the Date object.

1
2
    var mydate = new Date('6/20/2008');
    alert(mydate.Add("D", 10));

Tags: ,

Related Links

Leave a Reply

You must be logged in to post a comment.


cheap software