Javascript DateAdd function

  • SpiltBeer
  • Newbie
  • Newbie
  • SpiltBeer
  • Posts: 11
  • Loc: Madison

Post 3+ Months Ago

Is there an equivalent to vb's DateAdd function for java script. The main thing I need to do is to is add months to a date.

Thanks,
-Jesse
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

You can use the Date() object to get the month number and just add to it.

Code: [ Select ]
myDate = new Date();
month = myDate.getMonth();
modifiedMonth = month+x;
  1. myDate = new Date();
  2. month = myDate.getMonth();
  3. modifiedMonth = month+x;
  • SpiltBeer
  • Newbie
  • Newbie
  • SpiltBeer
  • Posts: 11
  • Loc: Madison

Post 3+ Months Ago

I don't think this will increment the months properly.

Code: [ Select ]

myDate = new Date(2005,12,31);
month = myDate.getMonth();
modifiedMonth = month+2;
modifiedDate = newDate(2005, modifiedMonth, 31);
  1. myDate = new Date(2005,12,31);
  2. month = myDate.getMonth();
  3. modifiedMonth = month+2;
  4. modifiedDate = newDate(2005, modifiedMonth, 31);


The date would be 14/31/2005 which when used to create a date becomes
3/3/2006. Ideally that would need to be 2/28(9)/2006. The VB DateAdd takes care of this. Java has the Calendar object. I've seen some javascript DateAdd scripts on the net but they don't take into account the end of the month.

Does anyone know if there is a simular function to DateAdd or a script somewhere?[/code]
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Maybe this will help...

http://slingfive.com/pages/code/jsDate/jsDate.html
  • SpiltBeer
  • Newbie
  • Newbie
  • SpiltBeer
  • Posts: 11
  • Loc: Madison

Post 3+ Months Ago

Thanks, this helps. Still doesn't show any love for end of month dates but I'll take what I can get.

-Jesse
  • enknot
  • Born
  • Born
  • enknot
  • Posts: 1

Post 3+ Months Ago

Ahhh yes the date issue. I find that I have quite a few issues w/ JavaScript, and to remedy them I've been writing language extensions and including them w/ every page I use so that don't ever have that problem again. Here's a neat lil piece of code a pieced toghether from some stuff I found on line and own knowledge. I forget where I got the bulk of the code, but I've seen this type of (or perhaps exact) function quite a few times before. I've changed it into an extention of the date object so that all your date object have it and can use it on the fly. Have fun:

Code: [ Select ]
function dateAddExtention(p_Interval, p_Number){


    var thing = new String();
    
    
    //in the spirt of VB we'll make this function non-case sensitive
    //and convert the charcters for the coder.
    p_Interval = p_Interval.toLowerCase();
    
    if(isNaN(p_Number)){
    
        //Only accpets numbers 
        //throws an error so that the coder can see why he effed up    
        throw "The second parameter must be a number. \n You passed: " + p_Number;
        return false;
    }

    p_Number = new Number(p_Number);
    switch(p_Interval.toLowerCase()){
        case "yyyy": {// year
            this.setFullYear(this.getFullYear() + p_Number);
            break;
        }
        case "q": {        // quarter
            this.setMonth(this.getMonth() + (p_Number*3));
            break;
        }
        case "m": {        // month
            this.setMonth(this.getMonth() + p_Number);
            break;
        }
        case "y":        // day of year
        case "d":        // day
        case "w": {        // weekday
            this.setDate(this.getDate() + p_Number);
            break;
        }
        case "ww": {    // week of year
            this.setDate(this.getDate() + (p_Number*7));
            break;
        }
        case "h": {        // hour
            this.setHours(this.getHours() + p_Number);
            break;
        }
        case "n": {        // minute
            this.setMinutes(this.getMinutes() + p_Number);
            break;
        }
        case "s": {        // second
            this.setSeconds(this.getSeconds() + p_Number);
            break;
        }
        case "ms": {        // second
            this.setMilliseconds(this.getMilliseconds() + p_Number);
            break;
        }
        default: {
        
            //throws an error so that the coder can see why he effed up and
            //a list of elegible letters.
            throw    "The first parameter must be a string from this list: \n" +
                    "yyyy, q, m, y, d, w, ww, h, n, s, or ms. You passed: " + p_Interval;
            return false;
        }
    }
    return this;
}
Date.prototype.dateAdd = dateAddExtention;

            //pay attention to the new. If you don't use it
            //you won't create a Date object, and that code up there won't work            
            var dToday = new Date();    
            
            //blamo!    
            alert(dToday.dateAdd("m", 2));
  1. function dateAddExtention(p_Interval, p_Number){
  2.     var thing = new String();
  3.     
  4.     
  5.     //in the spirt of VB we'll make this function non-case sensitive
  6.     //and convert the charcters for the coder.
  7.     p_Interval = p_Interval.toLowerCase();
  8.     
  9.     if(isNaN(p_Number)){
  10.     
  11.         //Only accpets numbers 
  12.         //throws an error so that the coder can see why he effed up    
  13.         throw "The second parameter must be a number. \n You passed: " + p_Number;
  14.         return false;
  15.     }
  16.     p_Number = new Number(p_Number);
  17.     switch(p_Interval.toLowerCase()){
  18.         case "yyyy": {// year
  19.             this.setFullYear(this.getFullYear() + p_Number);
  20.             break;
  21.         }
  22.         case "q": {        // quarter
  23.             this.setMonth(this.getMonth() + (p_Number*3));
  24.             break;
  25.         }
  26.         case "m": {        // month
  27.             this.setMonth(this.getMonth() + p_Number);
  28.             break;
  29.         }
  30.         case "y":        // day of year
  31.         case "d":        // day
  32.         case "w": {        // weekday
  33.             this.setDate(this.getDate() + p_Number);
  34.             break;
  35.         }
  36.         case "ww": {    // week of year
  37.             this.setDate(this.getDate() + (p_Number*7));
  38.             break;
  39.         }
  40.         case "h": {        // hour
  41.             this.setHours(this.getHours() + p_Number);
  42.             break;
  43.         }
  44.         case "n": {        // minute
  45.             this.setMinutes(this.getMinutes() + p_Number);
  46.             break;
  47.         }
  48.         case "s": {        // second
  49.             this.setSeconds(this.getSeconds() + p_Number);
  50.             break;
  51.         }
  52.         case "ms": {        // second
  53.             this.setMilliseconds(this.getMilliseconds() + p_Number);
  54.             break;
  55.         }
  56.         default: {
  57.         
  58.             //throws an error so that the coder can see why he effed up and
  59.             //a list of elegible letters.
  60.             throw    "The first parameter must be a string from this list: \n" +
  61.                     "yyyy, q, m, y, d, w, ww, h, n, s, or ms. You passed: " + p_Interval;
  62.             return false;
  63.         }
  64.     }
  65.     return this;
  66. }
  67. Date.prototype.dateAdd = dateAddExtention;
  68.             //pay attention to the new. If you don't use it
  69.             //you won't create a Date object, and that code up there won't work            
  70.             var dToday = new Date();    
  71.             
  72.             //blamo!    
  73.             alert(dToday.dateAdd("m", 2));
  • SpiltBeer
  • Newbie
  • Newbie
  • SpiltBeer
  • Posts: 11
  • Loc: Madison

Post 3+ Months Ago

Thanks for the replies.

The one thing that nails the code is the different ending of the months. You add one month to Jan 31st and you get March 3rd. I posted this on a different forum and this code was in one of the posts:

Code: [ Select ]
function addMonth(d,month){
t = new Date (d);
 t.setMonth(d.getMonth()+ month) ;
 if (t.getDate() < d.getDate())
{
   t.setDate(0);
 }
 return t;

}
  1. function addMonth(d,month){
  2. t = new Date (d);
  3.  t.setMonth(d.getMonth()+ month) ;
  4.  if (t.getDate() < d.getDate())
  5. {
  6.    t.setDate(0);
  7.  }
  8.  return t;
  9. }


Please note that this is not my code. The forum can be found here:
http://groups-beta.google.com/group/JavaScript-Information/browse_thread/thread/888e0b679f759293/470728c279c40888?hl=en#470728c279c40888

Thanks again
  • summerwu
  • Novice
  • Novice
  • summerwu
  • Posts: 19

Post 3+ Months Ago

Code: [ Select ]
<script language="JavaScript">
<!--
today=new Date();
function initArray(){
  this.length=initArray.arguments.length
  for(var i=0;i<this.length;i++)
  this[i+1]=initArray.arguments[i] }
  var d=new initArray(
   "Sunday",
   "Monday",
   "Thuesday",
   "Wednesday",
   "Thursday",
   "Friday",
   "Saturday");
    document.write(
        "<font style='font-size:9pt;font-family:Verdana'> ",
               today.getYear(),"/",
           today.getMonth()+1,"/",
           today.getDate(),"/   ",
           d[today.getDay()+1],
    "</font>" );
//--></script>
  1. <script language="JavaScript">
  2. <!--
  3. today=new Date();
  4. function initArray(){
  5.   this.length=initArray.arguments.length
  6.   for(var i=0;i<this.length;i++)
  7.   this[i+1]=initArray.arguments[i] }
  8.   var d=new initArray(
  9.    "Sunday",
  10.    "Monday",
  11.    "Thuesday",
  12.    "Wednesday",
  13.    "Thursday",
  14.    "Friday",
  15.    "Saturday");
  16.     document.write(
  17.         "<font style='font-size:9pt;font-family:Verdana'> ",
  18.                today.getYear(),"/",
  19.            today.getMonth()+1,"/",
  20.            today.getDate(),"/   ",
  21.            d[today.getDay()+1],
  22.     "</font>" );
  23. //--></script>

>you can just test it
>GOOD LUCK

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 162 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.