Return Date Parts With VBScript

Return Date Parts With VBScript

Browsers Targeted: Internet Explorer 4+

You can retrieve any component of a date (either as a string, a hash-delimited date expression, or a date object) with the DatePart() function in Visual Basic. As with both DateAdd() and DateDiff, you specify an interval token and the function returns the part of the date given by the token.

For example, suppose that you wanted to retrieve the current day of the week (with Sunday being 1). The code for this retrieval is quite simple:


The token intervals are given here:

Interval Token Interval Meaning Function Call
N/A Now Now
yyyy Year DatePart(“yyyy”,Now)
q Quarter DatePart(“q”,Now)
m Month DatePart(“m”,Now)
y Day in Year DatePart(“y”,Now)
w weekday DatePart(“w”,Now)
ww week in year DatePart(“ww”,Now)
h Hour DatePart(“h”,Now)
n Minute DatePart(“n”,Now)
s Second DatePart(“s”,Now)


Share the Post: