codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » ASP » Article

Calculating Date Differences with DateDiff

Article by: Art Downey (3/17/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Sponsored by: FindMyHosting - Web Hosting Search
Summary: A summary of the various ways of calculating the difference between two dates.
Viewed: 39336 times Rating (19 votes): 
 3.8 out of 5
 Rate this Article  Read Comments  Post Comments

Calculating Date Differences with DateDiff



To calculate the number of months between today and another date, use


Select All Code


Date here, of course, is the vbscript date function which returns todays date.

The "m" specifies month. Here are a complete list of the other options available.



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




CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Calculating Date Differences with DateDiff'
Posted by :  Archive Import (Joel Omeike) at 10:50 on Monday, May 19, 2003
Thanks a lot for this article.
I've been building an extensive online payment platform and had been having problems with the transaction history. your article just sorted this out. cheers,
joel
jomeike@mail.com
Posted by :  Archive Import (Hitul) at 14:16 on Thursday, June 12, 2003
date diff function
Posted by :  Archive Import (Tina Baker) at 11:32 on Tuesday, September 16, 2003
I found your article of interest, there is only one problem that I need to address rather than going in to lengthy code.

DiffADate = "Years old = " & DateDiff("yyyy", 24/09/1991, [16/09/2003])

[assume this is the now system Date]

In theory this should return "Years Old = 11" it returns 12, please advise
Posted by :  jennuhw at 15:14 on Tuesday, September 30, 2003
You are finding the difference between the years which is 12.
Posted by :  Opels at 04:02 on Monday, June 06, 2005
Hi,

I am using C#. I would like to calculate the difference between 2 dates and the result is expected to be in No. of Days. Refer to the below example

DateTime dt1 = dr[REC_DATE_COL]; //01/07/2005
DateTime dt2 = dr[DUE_DATE_COL]; //05/07/2005

TimeSpan ts1 = dt1 - dt2;
Messagebox.Show(ts1.Days.ToString()); //Result should be 4 Days

The result shown above in the form of messagebox is returning the result as 120.

Can anyone suggest anything. I'll greatly appreciate it.

Thanks in advance.

Regards.
Posted by :  murali_507 at 06:17 on Wednesday, August 10, 2005
Hi opels,

U try this way. The code is in C#


For Example

DateTime dt1= new DateTime(2005,7,1); //01/07/2005
DateTime dt2= new DateTime(2005,7,5); //05/07/2005


Messagebox.Show(dt2.Subtract(dt1).Days)// the result shows 4 as u would expect

Murali


<Added>

Messagebox.Show(Convert.ToString(dt2.Subtract(dt1).Days))//


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
MagicGrid
by Abhijeet Kaulgud
MagicGrid is an all-in-one grid for ASP programmers. It is a 3 Level Hierarchial Grid. You can Add, Edit, Delete Items under all the three levels. You can also cut-copy-paste Items from one level to other, It happens just by drag & drop!
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
ASP.NET Forum Source Code
by ITCN
Complete open source website Forum and Discussion Board programmed in Microsoft dot Net 1.1 Framework with Visual Basic.
The asp:listbox control
by David Sussman, et al
The next HTML server control that we'll look at, <asp:listbox>, is very much related to <asp:dropdownlist>.








Recent Forum Threads
• Re: sorting and Linked list
• Re: need help linked list
• Re: Help with arrays
• Re: Reading from a file
• Re: Why Use Method?
• Re: Help with a simple program
• Re: need help with quiz
• Re: Help with filesystem object & displaying in a table
• Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005