0

In VB there is a function called DateDiff(). But in C# it is not available. I want a function or any code in C# that can perform the same DateDiff function as in VB..

Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)

I want to do this in C#...

Ganesh Gaxy
  • 57
  • 1
  • 10
  • 2
    Why have you tagged html, javascript and jquery when you want someone who knows c# to answer the question? please retag. – Andrew Matthew Jul 30 '14 at 10:44
  • Sorry, I'm new to stackoverflow. How to Retag it?? – Ganesh Gaxy Jul 30 '14 at 10:45
  • click 'edit' at the bottom of your post, and from there you will see tags. Delete the tags there and put in c# instead – Andrew Matthew Jul 30 '14 at 10:46
  • 4
    Without trying to sound too much like a c#nt: did you try googling `c# datediff` ? Since it has code for this in all of the first 10 answers. – DoXicK Jul 30 '14 at 10:48
  • thanx for the link, i checked it just supports difference between two dates, i need entire method to work as DateDiff in VB, actually i had that before, but i lost. There is somekind of class is there.. – Ganesh Gaxy Jul 30 '14 at 10:48
  • 1
    Have a look at `DateTime`, `DateTime.Subtract` and `TimeSpan` (even in VB.net you should do it like this ..) – Random Dev Jul 30 '14 at 10:51
  • 2
    If you want to have exactly the same semantics, you *can* reference `Microsoft.VisualBasic` assembly in your C# code and then just use the `DateDiff` method from the [`DateAndTime`](http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateandtime(v=vs.110).aspx) class. – Damien_The_Unbeliever Jul 30 '14 at 10:51
  • @Damien_The_Unbeliever Ah you posted that comment just as I was adding an answer that says exactly the same thing... – Matthew Watson Jul 30 '14 at 10:53
  • i have found the class to use, no need to refer the assembly, there is a class for that, anyway thanx for all to your usefull comments – Ganesh Gaxy Jul 30 '14 at 10:57
  • thanx everyone. I solved this. But without understanding the question some guy downgraded the vote. Let him long live.. – Ganesh Gaxy Jul 30 '14 at 10:59
  • that guy name is Himanshu.MarJAVA who doesn't have time to read entire question – Ganesh Gaxy Jul 30 '14 at 11:00
  • I don't think this is a duplicate. That question is about number of days only – SysDragon Jul 31 '14 at 14:05

2 Answers2

1

You can create your own DateDiff() function:

public enum Intervals
{
    Days,
    Months,
    Years
}

public static int DateDiff(Intervals eInterval, System.DateTime dtInit, System.DateTime dtEnd)
{
    if (dtEnd < dtInit)
        throw new ArithmeticException("Init date should be previous to End date.");

    switch (eInterval) {
        case Intervals.Days:
            return (dtEnd.AddDays - dtInit).TotalDays;
        case Intervals.Months:
            return ((dtEnd.Year - dtInit.Year) * 12) + dtEnd.Month - dtInit.Month;
        case Intervals.Years:
            return dtEnd.Year - dtInit.Year;
        default:
            throw new ArgumentException("Incorrect interval code.");
    }
}
SysDragon
  • 9,692
  • 15
  • 60
  • 89
0
  1. Add to your C# project a reference to the .Net assembly Microsoft.VisualBasic
  2. Call Microsoft.VisualBasic.DateAndTime.DateDiff() with the appropriate parameters.
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276