VBScript and VB6 native Date functions emulated in Javascript
About
As a scripting language,
Javascript (aka
ECMAScript, aka
JScript)
is extremely powerful, but quite weak with date/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, with several intrinsic date/time-related functions, like
IsDate,
CDate,
DateAdd,
DateDiff,
DatePart,
MonthName,
WeekdayName, and
FormatDateTime.
Better yet is
VB6's
Format function.
Anyway, after banging my head on the wall for the nth time, I decided to try to re-create those functions in a Javascript include file,
called jsDate. Try it out...
Live Demo
(element relationships: related, active )
IsDate, DateAdd, and
DateDiff are the biggest helps for Javascript.  The rest are 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 in VBScript.
Limitations: in VBScript, DateDiff and DatePart() accept the
option parameter FirstWeekOfYear.
jsDate ignores this, and assumes Sunday & the week of January 1st (which are VBScript's defaults).
Enhancements: jsDate's DateAdd and
DateDiff functions can track Milliseconds (unlike VBScript). I've also added a
Format function emulating the same in VB6 (date-features only). This should fill the gaps in
FormatDateTime (which wraps Format anyway).
Other differences: jsDate's IsDate has different tolerances than VBScript's.
It allows otherwise invalid date values through (Javascript internally "corrects" the date, e.g. 1/32/2000 → 2/1/2000), but
disallows other date formats which VBScript accepts (e.g. yearless dates, dateless time, and dash or dot date separators).
I added the new CDate implementation for this reason.
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?).
maybe TODO: better documentation; make jsDate's IsDate tolerances more like VBScript's; implement FirstWeekOfYear parameter; implement Now; implement DateSerial/DateValue/Time/TimeSerial/TimeValue?;
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. Feedback and contributions are welcome and desired.