Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DateAdd() Function

    I was looking through a VBScript language reference (because I'm learning how to write WSH scripts and am using VBScript as my language of choice) and I found a nifty little function called DateAdd(). The function allows you to change a date object's time very easily.

    Noticing how this function was missing from JavaScript, I decided to write my own DateAdd() function for JavaScript.

    Code:
    function DateAdd(timeU,byMany,dateObj) {
    	var millisecond=1;
    	var second=millisecond*1000;
    	var minute=second*60;
    	var hour=minute*60;
    	var day=hour*24;
    	var year=day*365;
    
    	var newDate;
    	var dVal=dateObj.valueOf();
    	switch(timeU) {
    		case "ms": newDate=new Date(dVal+millisecond*byMany); break;
    		case "s": newDate=new Date(dVal+second*byMany); break;
    		case "mi": newDate=new Date(dVal+minute*byMany); break;
    		case "h": newDate=new Date(dVal+hour*byMany); break;
    		case "d": newDate=new Date(dVal+day*byMany); break;
    		case "y": newDate=new Date(dVal+year*byMany); break;
    	}
    	return newDate;
    }
    Note: you can also use this function to return a time value earlier than the date object given by using negative integers.

    Happy coding!

  • #2
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nolachrymose
    I was looking through a VBScript language reference (because I'm learning how to write WSH scripts and am using VBScript as my language of choice) and I found a nifty little function called DateAdd(). The function allows you to change a date object's time very easily.

    Noticing how this function was missing from JavaScript, I decided to write my own DateAdd() function for JavaScript.

    Code:
    function DateAdd(timeU,byMany,dateObj) {
    	var millisecond=1;
    	var second=millisecond*1000;
    	var minute=second*60;
    	var hour=minute*60;
    	var day=hour*24;
    	var year=day*365;
    
    	var newDate;
    	var dVal=dateObj.valueOf();
    	switch(timeU) {
    		case "ms": newDate=new Date(dVal+millisecond*byMany); break;
    		case "s": newDate=new Date(dVal+second*byMany); break;
    		case "mi": newDate=new Date(dVal+minute*byMany); break;
    		case "h": newDate=new Date(dVal+hour*byMany); break;
    		case "d": newDate=new Date(dVal+day*byMany); break;
    		case "y": newDate=new Date(dVal+year*byMany); break;
    	}
    	return newDate;
    }
    Note: you can also use this function to return a time value earlier than the date object given by using negative integers.

    Happy coding!
    Sorry, but the function doesn't work correctly. It's not the code but javascript that causes the problem. Most of the dates are correct but if you try, for instance, march 10 2005 with intervals of days and number of days as 7 it will come out as march 16 2005. it works from around march 12 onwards though. Dates suck don't they!!

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by cadaha
    Sorry, but the function doesn't work correctly. It's not the code but javascript that causes the problem. Most of the dates are correct but if you try, for instance, march 10 2005 with intervals of days and number of days as 7 it will come out as march 16 2005. it works from around march 12 onwards though. Dates suck don't they!!
    I've found something simple that seems to work
    Code:
      cseNewDate.setDate(cseNewDate.getDate() + 7);
    Hope this helps

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Quote Originally Posted by nolachrymose
    Noticing how this function was missing from JavaScript, I decided to write my own DateAdd() function for JavaScript.
    Probably the reason why DateAdd doesn't exist in JavaScript is because JavaScript already have these methods that can do what DateAdd can.

    getMilliseconds(), setMilliseconds()
    getSeconds(), setSeconds()
    getMinutes(), setMinutes()
    getHours(), setHours()
    getDate(), setDate()
    getMonth(), setMonth()
    getFullYear(), setFullYear()

    But if a similar method is added to JavaScript, this is how I'd do it:

    Code:
    Date.prototype.add = function (sInterval, iNum){
      var dTemp = this;
      if (!sInterval || iNum == 0) return dTemp;
      switch (sInterval.toLowerCase()){
        case "ms":
          dTemp.setMilliseconds(dTemp.getMilliseconds() + iNum);
          break;
        case "s":
          dTemp.setSeconds(dTemp.getSeconds() + iNum);
          break;
        case "mi":
          dTemp.setMinutes(dTemp.getMinutes() + iNum);
          break;
        case "h":
          dTemp.setHours(dTemp.getHours() + iNum);
          break;
        case "d":
          dTemp.setDate(dTemp.getDate() + iNum);
          break;
        case "mo":
          dTemp.setMonth(dTemp.getMonth() + iNum);
          break;
        case "y":
          dTemp.setFullYear(dTemp.getFullYear() + iNum);
          break;
      }
      return dTemp;
    }
    
    //sample usage
    var d = new Date();
    var d2 = d.add("d", 3); //+3days
    var d3 = d.add("h", -3); //-3hours
    alert(d2);
    alert(d3);

  • #5
    New to the CF scene
    Join Date
    Aug 2009
    Location
    Singapore
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If any of you interested in having DateDiff, and DateAdd in VB way please have a look here

    Javascript Date functions for VB DateDiff DateAdd


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •