Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello all

I need to have a replacement of DateDiff method in C#..... I have translate some vb code into C# and have an error in DateDiff...

Below is my VB Code
VB
If rs("test_date") > Now() Then
                       sb.Append(DateDiff("d", Now(), rs("test_date")) & " days away")
                   Else


Below is C# code

C#
if (Convert.ToDateTime(rs["test_date"]) > DateTime.Now)
               {
                   sb.Append(DateDiff("d", DateTime.Now, rs["test_date"]) + " days away");
               }


Error:
sb.Append(DateDiff("d", DateTime.Now, rs["test_date"]) + " days away");
error name: The name 'DateDiff' does not exists in current context.

You all are requested to let me have a replacement of this...

Thanks
Posted
Updated 14-Jun-15 19:44pm
v4

Instead of DateDiff, use a TimeSpan[^]:
C#
TimeSpan diff = (DateTime)rs["test_date"] - DateTime.Now;
int daysAway = diff.TotalDays;
sb.AppendFormat("{0} days away", daysAway);
 
Share this answer
 
Well, I wouldn't do it, but I suppose you could add a reference to

Namespace: Microsoft.VisualBasic

Module: DateAndTime

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

https://msdn.microsoft.com/en-us/library/b5xbyt6f(v=vs.90).aspx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900