DateAdd: Difference between revisions
Jump to navigation
Jump to search
Add javascript snippet |
|||
Line 32: | Line 32: | ||
|} | |} | ||
== Example == | == Example (Basic) == | ||
<pre> | <pre> | ||
Line 39: | Line 39: | ||
Print "+10 seconds:", DateAdd("s", 10, NOW) | Print "+10 seconds:", DateAdd("s", 10, NOW) | ||
Print "-1 year:", DateAdd("yyyy", -1, NOW) | Print "-1 year:", DateAdd("yyyy", -1, NOW) | ||
</pre> | |||
== Example (JavaScript) == | |||
<pre> | |||
// DateAdd Example | |||
/* DateAdd adds date intervals to a date */ | |||
Date.prototype.DateAdd = function(typ,val) { | |||
dt=this; | |||
if (!typ || val==0) return dt; | |||
switch (typ.toLowerCase()) { | |||
case 'yyyy': | |||
dt.setFullYear(dt.getFullYear()+val); break; | |||
case 'q': | |||
var nm = dt.getMonth()+(val*3); | |||
dt.setMonth(dt.getMonth()+(val*3)); | |||
while (dt.getMonth() != nm) { | |||
dt.setDate(dt.getDate()-1); | |||
} | |||
break; | |||
case 'm': | |||
var nm = dt.getMonth()+val; | |||
dt.setMonth(dt.getMonth()+val); | |||
while (dt.getMonth() != nm) { | |||
dt.setDate(dt.getDate()-1); | |||
} | |||
break; | |||
case 'y': | |||
case 'd': | |||
case 'w': | |||
dt.setDate(dt.getDate()+val); break; | |||
case 'ww': | |||
dt.setDate(dt.getDate()+(val*7)); break; | |||
case 'h': | |||
dt.setHours(dt.getHours()+val); break; | |||
case 'n': | |||
dt.setMinutes(dt.getMinutes()+val); break; | |||
case 's': | |||
dt.setSeconds(dt.getSeconds()+val); break; | |||
} | |||
return dt; | |||
} | |||
var dt = new Date(); | |||
NSB.Print('+10 seconds: ' + dt.DateAdd('s', 10)); | |||
NSB.Print('add 1: ' + dt.DateAdd('ww', 1)); | |||
</pre> | </pre> | ||
Revision as of 21:00, 26 May 2013
DateAdd(interval,number,date)
Description
DateAdd returns the date which is obtained by adding a specified number of date intervals to a given date. The required parameter, interval, is a string expression that specifies the type of date interval to add: see table below. The required parameter, number, is any numeric expression that specifies the number of intervals to add.The required parameter, date, can be any expression that represents a date.
Interval Values
Value | Description |
---|---|
yyyy | Year |
q | Quarter |
m | Month |
y | Day of year |
d | Day |
w | Weekday |
ww | Week of year |
h | Hour |
n | Minute |
s | Second |
Example (Basic)
Rem DateAdd Example 'DateAdd adds date intervals to a date Print "+10 seconds:", DateAdd("s", 10, NOW) Print "-1 year:", DateAdd("yyyy", -1, NOW)
Example (JavaScript)
// DateAdd Example /* DateAdd adds date intervals to a date */ Date.prototype.DateAdd = function(typ,val) { dt=this; if (!typ || val==0) return dt; switch (typ.toLowerCase()) { case 'yyyy': dt.setFullYear(dt.getFullYear()+val); break; case 'q': var nm = dt.getMonth()+(val*3); dt.setMonth(dt.getMonth()+(val*3)); while (dt.getMonth() != nm) { dt.setDate(dt.getDate()-1); } break; case 'm': var nm = dt.getMonth()+val; dt.setMonth(dt.getMonth()+val); while (dt.getMonth() != nm) { dt.setDate(dt.getDate()-1); } break; case 'y': case 'd': case 'w': dt.setDate(dt.getDate()+val); break; case 'ww': dt.setDate(dt.getDate()+(val*7)); break; case 'h': dt.setHours(dt.getHours()+val); break; case 'n': dt.setMinutes(dt.getMinutes()+val); break; case 's': dt.setSeconds(dt.getSeconds()+val); break; } return dt; } var dt = new Date(); NSB.Print('+10 seconds: ' + dt.DateAdd('s', 10)); NSB.Print('add 1: ' + dt.DateAdd('ww', 1));
Output
+10 seconds: 8/18/98 10:52:54 PM -1 year: 8/18/97 10:52:44 PM