devxlogo

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)

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.