About
Download
Pricing/Purchase Info
Highlights
FAQ
How-To
Tutorials
Learn ASP Express
Tech Support
Bug Report
Hosting
Resources
Home
Forums

Peace - Now and Forever
ASP Express
More than just an editor
ASP Express Home | What You Get | Pricing Information

New-->Online User Count
Using DatePart and other Date Functions
Understanding Includes
(Updated) Using Variables, Single & DoubleQuotes with SQL Statements in ASP
(Updated) Emailing Form Results
Mass Emailing with CDO
Using The Ad Rotator
ASCII Character Set 0-127
Datepart
Using Date & DatePart, and Others

There are different intervals you can use when you use DatePart (listed at bottom of page), along with several constants (which I won't get into on this page). DatePart returns part of a specified date in this form:
DatePart(interval, date[, firstdayofweek[, firstweekofyear]])

List of Arguments
interval
Required. String expression that is the interval of time you want to return. See Settings section for values.
date
Required. Date expression you want to evaluate.
firstdayof week
Optional. Constant that specifies the day of the week. If not specified, Sunday is assumed. See Settings section for values.
firstweekofyear
Optional. Constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs. See Settings section at the bottom of the page for useable values.

Below are examples of some different ways to use DatePart, along with other popular date-type functions. Below each example is the code which creates it.

Here is today's date (using Date) : 12/11/2018

The month's name is December

<% 
CurrentMonth=DatePart("m",date)
Response.Write("<B>The month's  name is </B> " & MonthName(currentmonth))
%>

Number of the month : 12

<% response.Write "<B>Number of the month </B> : " & Currentmonth %>;


Here is the abbreviated month : Dec

<% 
Response.Write monthname(currentmonth, true) 
%>


The year is : 2018

<% 
MyYear=DatePart("yyyy", date)
response.Write "<B>The year is </B> : " & MyYear 
%>


Today's date is :11

<% 
MyWeekday=DatePart("d", date)
response.Write "Today's date is  :" & MyWeekday 
%>


Here is the Day of the week : 3

<% 
MyWeekday=DatePart("w", date)
Response.Write MyWeekday 
%>


Here is the Week of the Year : 50

<% 
MyWeekday=DatePart("ww", date)
Response.Write MyWeekday 
%>


The day of the year is : 345
(Which means that here are 20 Days Left this year)

<% 
MyYear=DatePart("y", date)
response.Write "<B>The day of the year is </B> : " & MyYear 
Left=(365-MyYear)
response.Write "<br>(<I>Which means that here are " & left & " Days Left this year</I>)"
%>


Intervals and Constants for DatePart

Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week of year
h Hour
n Minute
s Second

The firstdayofweek argument can have any the following values:

Constant Value Description
vbUseSystem 0 Use National Language Support (NLS) API setting.
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday

Back to the Top | Tutorials Home | ASP Express Home

Send your comments, questions or suggestions via E-Mail to : (suggestions@aspexpress.com)