VBScript native Date functions emulated in Javascript
About
As a scripting language,
Javascript (aka
ECMAScript, aka
JScript
is extremely powerful, yet it is pitifully weak with date and time manipulation. Several types of date/time operations are very difficult to accomplish (and accurately) in Javascript.
VBScript is comparatively very strong in that area, though, possessing several native date/time-related functions, like
isDate,
dateAdd,
dateDiff,
datePart,
monthName and
weekdayName. I've re-created and packaged those functions for Javascript in an include file, called jsDate.
Check it out...
isDate, dateAdd, and
dateDiff are the biggest helps for Javascript.  The others are just icing.
Compatibility: the functions should work in any modern browser. The demo itself works in IE and Firefox at least.
Usage: copy the file to your project, link to it, and you can begin using the functions just as would in VBScript.
Limitations: in VBScript, DateDiff and DatePart() accept the option arguments
firstdayofweek and firstweekofyear.
jsDate ignores those arguments, and assumes Sunday and the week of January 1st (which is default in VBScript).
Enhancements: VBScript is not capable of dealing with Milliseconds.
Javascript can, though, so I enabled that extended interval for jsDate's dateAdd and dateDiff functions.
Other differences: jsDate's isDate is less strict than VBScript's, and may let otherwise invalid dates through (this is because Javascript internally "corrects" the date, e.g. 1/32/2000 --> 2/1/2000).
It turns out that VBScript's DateDiff does not check that each YYYY, M, or Q interval has been completed, but just if it has rolled over into a new one. So in VBScript, DateDiff("yyyy", "12/31/2000", "1/1/2001") returns 1 (even though it's just 1 day completed, not a year). As of v0.94, jsDate's DateDiff now behaves this way too. One exception is the Q interval, which still checks for 3 months completed (does anyone really use Q?).
TODO: FormatDateTime (anyone have a good object-based one to contribute?); ~isDate2 - strict like VBScript
history
2005-08-04 - v0.94: scrapped 0.93b's new dateDiff interval code to better match VBScript's simplistic approach misc tweaks to this page
2005-08-03 - v0.93b: fixed dateDiff/leapyear bug with YYYY, M, & Q intervals (thanks Rolando and Dr. John for catching this)
2004-11-26 - v0.91: fixed datePart/ww bug, added weekdayName & monthName, first published
2004-08-30 - v0.9: brand new
support
As-is, etc. If you ask nicely, I may provide support via email.
credits/author
-Rob (@slingfive) Eberhardt, Slingshot Solutions
Use freely, but be honest about it. I just ask for credit.