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




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com