.NETGURU
Simple C# DateDiff()
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcodegiveawayswap' list.


Robert Chartier

I just threw this one together quickly...tell me how it works out.

/// <summary>
/// same common params as the VBScript DateDiff:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsfctdatediff.asp
/// /*Sample Code:
/// * System.DateTime dt1 = new System.DateTime(1974,12,16);
/// * System.DateTime dt2 = new System.DateTime(1973,12,16);
/// * Page.Response.Write(Convert.ToString(DateDiff("t", dt1, dt2)));
/// * */
/// </summary>
/// <param name="howtocompare"></param>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
private double DateDiff(string howtocompare, System.DateTime
startDate, System.DateTime endDate) {
double diff=0;
try {
System.TimeSpan TS = new
System.TimeSpan(startDate.Ticks-endDate.Ticks);
#region converstion options
switch (howtocompare.ToLower()) {
case "m":
diff = Convert.ToDouble(TS.TotalMinutes);
break;
case "s":
diff = Convert.ToDouble(TS.TotalSeconds);
break;
case "t":
diff = Convert.ToDouble(TS.Ticks);
break;
case "mm":
diff = Convert.ToDouble(TS.TotalMilliseconds);
break;
case "yyyy":
diff = Convert.ToDouble(TS.TotalDays/365);
break;
case "q":
diff = Convert.ToDouble((TS.TotalDays/365)/4);
break;
default:
//d
diff = Convert.ToDouble(TS.TotalDays);
break;
}
#endregion
} catch(Exception e) {
diff = -1;
}
return diff;
}

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.santra.com
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier
http://www.aspfree.com/devlinks
http://www.aspfree.com/authors/robert
http://www.aspfriends.com/aspfriends/fanclub-robertchartier.asp

Reply to this message...
 
    
Duncan Mackenzie
If you include a reference to Microsoft.VisualBasic and a "using"
directive on your page, you could use the DateDiff function defined
within that class library.

=20
Duncan Mackenzie (MSDN)
Click here to reveal e-mail address
www.duncanmackenzie.net
=20
=20

-----Original Message-----
From: Robert Chartier [mailto:Click here to reveal e-mail address]=20
Sent: Monday, January 28, 2002 11:26 PM
To: aspngcodegiveawayswap
Subject: [aspngcodegiveawayswap] Simple C# DateDiff()

I just threw this one together quickly...tell me how it works out.

/// <summary>
/// same common params as the VBScript DateDiff:=20
http://msdn.microsoft.com/library/default.asp?url=3D/library/en-us/script=
5
6/html/vsfctdatediff.asp
/// /*Sample Code:
/// * System.DateTime dt1 =3D new System.DateTime(1974,12,16);
/// * System.DateTime dt2 =3D new System.DateTime(1973,12,16);
/// * Page.Response.Write(Convert.ToString(DateDiff("t", dt1,
dt2)));
/// * */
/// </summary>
/// <param name=3D"howtocompare"></param>
/// <param name=3D"startDate"></param>
/// <param name=3D"endDate"></param>
/// <returns></returns>
private double DateDiff(string howtocompare, System.DateTime=20
startDate, System.DateTime endDate) {
double diff=3D0;
try {
System.TimeSpan TS =3D new=20
System.TimeSpan(startDate.Ticks-endDate.Ticks);
#region converstion options
switch (howtocompare.ToLower()) {
case "m":
diff =3D Convert.ToDouble(TS.TotalMinutes);
break;
case "s":
diff =3D Convert.ToDouble(TS.TotalSeconds);
break;
case "t":
diff =3D Convert.ToDouble(TS.Ticks);
break;
case "mm":
diff =3D Convert.ToDouble(TS.TotalMilliseconds);
break;
case "yyyy":
diff =3D Convert.ToDouble(TS.TotalDays/365);
break;
case "q":
diff =3D Convert.ToDouble((TS.TotalDays/365)/4);
break;
default:
//d
diff =3D Convert.ToDouble(TS.TotalDays);
break;
}
#endregion
} catch(Exception e) {
diff =3D -1;
}
return diff;
}

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.santra.com
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier
http://www.aspfree.com/devlinks http://www.aspfree.com/authors/robert
http://www.aspfriends.com/aspfriends/fanclub-robertchartier.asp

| [aspngcodegiveawayswap] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.aspfriends.com/aspfriends/aspngcodegiveawayswap.asp =3D=20
| JOIN/QUIT

Reply to this message...
 
    
Paul D. Murphy
I hate to rain on your function, but if you want a TimeSpan you can just
subtract one DateTime object from another.

DateTime currentTime =3D DateTime.Now;
DateTime futureTime =3D DateTime.Now.AddHours(2);

TimeSpan howMuchTime =3D (futureTime - currentTime);

Paul

-----Original Message-----
From: Duncan Mackenzie [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, January 30, 2002 1:32 AM
To: aspngcodegiveawayswap
Subject: [aspngcodegiveawayswap] RE: Simple C# DateDiff()

If you include a reference to Microsoft.VisualBasic and a "using"
directive on your page, you could use the DateDiff function defined
within that class library.

=20
Duncan Mackenzie (MSDN)
Click here to reveal e-mail address
www.duncanmackenzie.net
=20
=20

-----Original Message-----
From: Robert Chartier [mailto:Click here to reveal e-mail address]=20
Sent: Monday, January 28, 2002 11:26 PM
To: aspngcodegiveawayswap
Subject: [aspngcodegiveawayswap] Simple C# DateDiff()

I just threw this one together quickly...tell me how it works out.

/// <summary>
/// same common params as the VBScript DateDiff:=20
http://msdn.microsoft.com/library/default.asp?url=3D/library/en-us/script=
5
6/html/vsfctdatediff.asp
/// /*Sample Code:
/// * System.DateTime dt1 =3D new System.DateTime(1974,12,16);
/// * System.DateTime dt2 =3D new System.DateTime(1973,12,16);
/// * Page.Response.Write(Convert.ToString(DateDiff("t", dt1,
dt2)));
/// * */
/// </summary>
/// <param name=3D"howtocompare"></param>
/// <param name=3D"startDate"></param>
/// <param name=3D"endDate"></param>
/// <returns></returns>
private double DateDiff(string howtocompare, System.DateTime=20
startDate, System.DateTime endDate) {
double diff=3D0;
try {
System.TimeSpan TS =3D new=20
System.TimeSpan(startDate.Ticks-endDate.Ticks);
#region converstion options
switch (howtocompare.ToLower()) {
case "m":
diff =3D Convert.ToDouble(TS.TotalMinutes);
break;
case "s":
diff =3D Convert.ToDouble(TS.TotalSeconds);
break;
case "t":
diff =3D Convert.ToDouble(TS.Ticks);
break;
case "mm":
diff =3D Convert.ToDouble(TS.TotalMilliseconds);
break;
case "yyyy":
diff =3D Convert.ToDouble(TS.TotalDays/365);
break;
case "q":
diff =3D Convert.ToDouble((TS.TotalDays/365)/4);
break;
default:
//d
diff =3D Convert.ToDouble(TS.TotalDays);
break;
}
#endregion
} catch(Exception e) {
diff =3D -1;
}
return diff;
}

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.santra.com
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier
http://www.aspfree.com/devlinks http://www.aspfree.com/authors/robert
http://www.aspfriends.com/aspfriends/fanclub-robertchartier.asp

| [aspngcodegiveawayswap] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.aspfriends.com/aspfriends/aspngcodegiveawayswap.asp =3D=20
| JOIN/QUIT

| [aspngcodegiveawayswap] member Click here to reveal e-mail address =3D YOUR ID
| http://www.aspfriends.com/aspfriends/aspngcodegiveawayswap.asp =3D
JOIN/QUIT

Reply to this message...
 
    
Robert Chartier

Thats basically what the method is doing, except its subtracting Ticks,
which is most likely the default property (?).

It simplifies pulling off the proper date/time values out of the TimeSpan
object.

Also, creating a Reference to VB is NOT a solution here. IMHO the
framework should not need to import anything from the old days. :)

/rob

At 08:48 AM 1/30/2002 -0500, you wrote:
[Original message clipped]

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.santra.com
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier
http://www.aspfree.com/devlinks
http://www.aspfree.com/authors/robert
http://www.aspfriends.com/aspfriends/fanclub-robertchartier.asp

Reply to this message...
 
    
Duncan Mackenzie
We won't discuss this for long, since that is not the purpose of the
list... But I agree, you shouldn't need to import anything from the old
days, but the Microsoft.VisualBasic namespace is a .NET implementation
of functions that people have become used to from VBScript and VB. It
is, essentially, doing exactly what you did.... Likely in C# just like
you, but written by the .NET team.

Duncan Mackenzie (MSDN)
Click here to reveal e-mail address
www.duncanmackenzie.net

-----Original Message-----
From: Robert Chartier [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, January 30, 2002 8:44 AM
To: aspngcodegiveawayswap
Subject: [aspngcodegiveawayswap] RE: Simple C# DateDiff()

Thats basically what the method is doing, except its subtracting Ticks,=20
which is most likely the default property (?).

It simplifies pulling off the proper date/time values out of the
TimeSpan=20
object.

Also, creating a Reference to VB is NOT a solution here. IMHO the=20
framework should not need to import anything from the old days. :)

/rob

At 08:48 AM 1/30/2002 -0500, you wrote:
[Original message clipped]

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.santra.com
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier
http://www.aspfree.com/devlinks http://www.aspfree.com/authors/robert
http://www.aspfriends.com/aspfriends/fanclub-robertchartier.asp

| [aspngcodegiveawayswap] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.aspfriends.com/aspfriends/aspngcodegiveawayswap.asp =3D=20
| JOIN/QUIT

Reply to this message...
 
 
System.Convert
System.DateTime
System.TimeSpan
System.Web.UI.Page




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification