Codefixer - ASP tutorials, resources and software
ASP.NET 3.5/2.0 Hosting – Click Here
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Monday 12 May
MembersPro PayPal - ASP Membership software

asp.netPRO “Best ASP.NET Host” – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

The DateDiff, DateAdd and DatePart VBScript Functions

The Datediff function

The Datediff function is a great way to calculate the interval period between two dates. If you set the interval parameter to m for months the function will return the amount of months between the two dates. The table below lists the interval periods you can set for the interval parameter.

Syntax: DateDiff(Interval,date1,date2)

Interval 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

<%
Response.write DateDiff("m","01/01/04",date) & " Months"
'date function returns todays date so its difference between 01/01/04 & today
%>

Output: 52 Months

The DateAdd function

The DateAdd() functions allows us to add or subtract from a date or time. DateAdd returns a date or time that has been modified by the number parameter.

Syntax: DateAdd(Interval, number, Date)

The interval argument accepts the following values:

Interval 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

<%
Response.write DateAdd("m",2,date)
'the date function returns today's date so we are adding 2 months on
'you can also substract i.e. -2
%>

Output: 12/07/2008
'this is UK format as the server is in the UK

<%
Response.write DateAdd("n",-25,time)
'the time function returns today's time so we are substracting 25 minutes
%>

Output: 04:32:14

The DatePart function

We can use the DatePart function to return the specified part of the date and time.

Syntax: DatePart(GetPartOftheDate, Date)

The GetPartoftheDate argument accepts the following values:

GetPartoftheDate 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

<%
Response.write "The Now function returns:" & Now & "<br>"
Response.write "Retrieve the year: " & DatePart("yyyy", Now)
%>

OutPut: The Now function returns:12/05/2008 04:57:14
Retrieve the year: 2008



Other date and time related tutorials:
Here's a code snippet to display the day and date (eg. Mon 25 December).
Here's a code snippet to display the day and date with the ordinal suffix (eg. Mon 25th December).
How to get the current month name
Display the dates and times
Time and Date based greetings
IsDate Function

LinksPro - Directory  and Link  Management Software




ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software