Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Web, Visual Basic
Expertise: Intermediate
Mar 9, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

<script language="VBScript">
	document.write "Current Date:"+cstr(curDate)+"<br/>"
    document.write "Day of Week: "+cstr(datePart("w",Now))

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)
Kurt Cagle
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date