nolachrymose
08-13-2002, 04:16 PM
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.
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! :)
cadaha
03-09-2005, 12:58 AM
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.
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!!
cadaha
03-09-2005, 01:16 AM
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
cseNewDate.setDate(cseNewDate.getDate() + 7);
Hope this helps :)
glenngv
03-14-2005, 05:01 AM
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:
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);
pons_saravanan
08-26-2009, 04:40 AM
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 (http://www.vbknowledgebase.com/WebApp/Guest/Home/Home.aspx?PageCode=80&Desc=Javascript-Date-functions-for-VB-DateDiff-DateAdd)