When you think ASP, think...
 Recent Articles All Articles ASP.NET Articles ASPFAQs.com Message Board Related Web Technologies User Tips! Coding Tips

 Sections: Sample Chapters Commonly Asked Message Board Questions JavaScript Tutorials MSDN Communities Hub Official Docs Security Stump the SQL Guru! XML Info Information: Feedback Author an Article

 Jump to a FAQ Enter FAQ #: ..or see our 10 Most Viewed FAQs.

# 4GuysFromRolla.com : ASP FAQS : Dates and Times

Question:

## How can I calculate the years, months, and days between two dates?

[Print this FAQ]

Answer: Performing date-related calculations in VBScript is really a breeze thanks to the myriad of helpful date-related functions in the language. There have, in fact, been a wide array of articles on the many powerful VBScript date functions. For a quick rundown, check out:

-- Using Date Functions (Part 1)
-- Using Date Functions (Part 2)
-- Using VBScript's Date Function
-- FAQ: How can I add or subtract time from a date?

One common task developer's want to do, is given two dates, calculate the number of years, days, months, or whatever between the two dates. Fortunately, VBScript provides a nice function to do this called DateDiff. (View the technical docs for DateDiff.) In its simplest form, DateDiff takes three input parameters:

* interval - specifies the date interval difference you are interested in (such as the months between two dates, or the weeks between dates, etc.)
* date1 - the first date
* date2 - the second date

The interval can be one of the following string values:

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

So if we have two dates, the current date and January 1, 2000, and we wanted to calculate the total number of days between the two, we could use the following code:

 Dim dtNow, dtY2KdtNow = Date()dtY2K = DateSerial(2000, 1, 1)Dim iDaysDifferenceiDaysDifference = DateDiff("d", dtY2K, dtNow)

This will return the total number of days that have elapsed since the two dates. That is, if dtNow was equal to Feb. 3rd, 2002, the result would be: 764.

FAQ posted by Scott Mitchell at 2/4/2002 12:05:46 AM to the Dates and Times category. This FAQ has been viewed 72,979 times.

 Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions! Most Viewed FAQs: 1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views) 2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views) 3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views) 4.) How can I quickly sort a VBScript array? (196039 views) 5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views) 6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views) 7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views) 8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views) 9.) Can I send emails without using CDONTS? (107083 views) 10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views) Last computed at 9/17/2007 3:22:00 AM

 ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article