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
Summary: A summary of the various ways of calculating the difference between two dates.
Viewed: 103803 times Rating (23 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))//
Posted by :  plsaiuser at 22:23 on Monday, March 13, 2006
Greetings All,

in the following code am using Microsoft.VisualBasic in my C# program. with out using Microsoft.VisualBasic can any one help me to give the complete equvalent code in C#

Microsoft.VisualBasic.FirstDayOfWeek z1 = 0;
DateTime dt1= DateTime.Now;
string strDayName = DateAndTime.WeekdayName(DateAndTime.Weekday(dt1,z1),false,z1).ToString();

Thanks
Tec
Posted by :  aliasunway at 10:28 on Friday, June 02, 2006
hai.. i'm trying to do the same thing using C++..
can anyone help me ? i'm juz a beginner.. tried so many things but nothing works..
Pleasssss...


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

 



RELATED ARTICLES
ASP FilesystemObject
by Jeff Anderson
An introduction to the Filesystemobject
ASP GetTempName
by Jeff Anderson
Use the GetTempName method to create a randomly generated temporary file on the server.
ASP OpenTextFile
by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject
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.
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 a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).
ASP FileExists
by Jeff Anderson
An introduction to the FileExistsMethod of the FileSystemObject
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
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.








Recent Forum Threads
• Regular Expression Query Replace -- Twice Wrongly
• Processing MS Office documents with Java
• Re: help!
• Re: Can javascript preload swf files?
• ADODB.Recordset error (0x800A0CB3)
• Need Your Inputs About Level Editor Interface
• `section` is an unexpected token. Expecting white space. Line 1, position 137.
• Javascript rollover toggle help please!
• help monitoting cpu load on windows machine


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009