Use VB DateDiff function in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andyoye

    Use VB DateDiff function in C#

    I have two date fields(startDat e, endDate) in a form and I want to
    calculate number of days between startDate & endDate.

    Thanks


  • Alberto Poblacion

    #2
    Re: Use VB DateDiff function in C#

    "andyoye" <andyoye@nospam .comwrote in message
    news:O4Oql4KJJH A.4248@TK2MSFTN GP05.phx.gbl...
    >I have two date fields(startDat e, endDate) in a form and I want to
    >calculate number of days between startDate & endDate.
    int days = ((TimeSpan)(end Date-startDate)).Day s;


    Comment

    • Cor Ligthert[MVP]

      #3
      Re: Use VB DateDiff function in C#

      Hi Andy,

      DateDiff in VB is a little bit from pre history.

      This it can be in VB 2008
      \\\
      Dim EndDate = DateTime.Now
      Dim StartDate = Now.AddDays(-3)
      Dim days = EndDate - StartDate
      MessageBox.Show (days.Days.ToSt ring)
      ///

      This it can be in C# 2008
      \\\\
      var EndDate = DateTime.Now;
      var StartDate = DateTime.Now.Ad dDays(-3);
      var days = EndDate - StartDate;
      MessageBox.Show (days.Days.ToSt ring());
      ///

      I real don't see much differences.

      The VB code only in this case to show that the question is a little bit
      strange.

      Cor


      "andyoye" <andyoye@nospam .comschreef in bericht
      news:O4Oql4KJJH A.4248@TK2MSFTN GP05.phx.gbl...
      >I have two date fields(startDat e, endDate) in a form and I want to
      >calculate number of days between startDate & endDate.
      >
      Thanks
      >

      Comment

      • andyoye

        #4
        Re: Use VB DateDiff function in C#

        startDate field value is 10-01-09
        endDate field value is 10-05-09

        How can I get 4 .....(endDate-startDate)

        "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
        news:umQG1LLJJH A.1160@TK2MSFTN GP05.phx.gbl...
        Hi Andy,
        >
        DateDiff in VB is a little bit from pre history.
        >
        This it can be in VB 2008
        \\\
        Dim EndDate = DateTime.Now
        Dim StartDate = Now.AddDays(-3)
        Dim days = EndDate - StartDate
        MessageBox.Show (days.Days.ToSt ring)
        ///
        >
        This it can be in C# 2008
        \\\\
        var EndDate = DateTime.Now;
        var StartDate = DateTime.Now.Ad dDays(-3);
        var days = EndDate - StartDate;
        MessageBox.Show (days.Days.ToSt ring());
        ///
        >
        I real don't see much differences.
        >
        The VB code only in this case to show that the question is a little bit
        strange.
        >
        Cor
        >
        >
        "andyoye" <andyoye@nospam .comschreef in bericht
        news:O4Oql4KJJH A.4248@TK2MSFTN GP05.phx.gbl...
        >>I have two date fields(startDat e, endDate) in a form and I want to
        >>calculate number of days between startDate & endDate.
        >>
        >Thanks
        >>
        >

        Comment

        • Jeff Johnson

          #5
          Re: Use VB DateDiff function in C#

          "andyoye" <andyoye@nospam .comwrote in message
          news:ekPoOSLJJH A.5696@TK2MSFTN GP06.phx.gbl...
          startDate field value is 10-01-09
          endDate field value is 10-05-09
          >
          How can I get 4 .....(endDate-startDate)
          Alberto gave you the answer.


          Comment

          • andyoye

            #6
            Re: Use VB DateDiff function in C#

            Below is smaple code from my form

            {

            1: XPathNavigator docFM = this.CreateNavi gator();

            2: string date_form = docFM.SelectSin gleNode("/my:myFields/my:startDate",
            this.NamespaceM anager).Value;

            3: XPathNavigator docFN = this.CreateNavi gator();

            4: string date_to = docFN.SelectSin gleNode("/my:myFields/my:startTime",
            this.NamespaceM anager).Value;

            5: DateTime startDate = Convert.ToDateT ime(date_frorm) ;

            6: DateTime endDate = Convert.ToDateT ime(date_to);

            7: TimeSpan dateDifference = endDate.Subtrac t(startDate);

            8: int days = dateDifference. Days;

            9: string daysString = days.ToString() ;

            10: XPathNavigator dateDiffTextBox Value =
            this.CreateNavi gator().SelectS ingleNode("/my:myFields/my:dateDiff",
            this.NamespaceM anager);

            11: dateDiffTextBox Value.SetValue( daysString);

            }

            I get
            "String was not recognized as a valid DateTime." at line 6
            "When convertin a string to DateTime, parse the string to take the date
            before putting each variable into the DateTime object"

            thx



            "Jeff Johnson" <i.get@enough.s pamwrote in message
            news:eicRybLJJH A.1968@TK2MSFTN GP06.phx.gbl...
            "andyoye" <andyoye@nospam .comwrote in message
            news:ekPoOSLJJH A.5696@TK2MSFTN GP06.phx.gbl...
            >
            >startDate field value is 10-01-09
            >endDate field value is 10-05-09
            >>
            >How can I get 4 .....(endDate-startDate)
            >
            Alberto gave you the answer.
            >

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: Use VB DateDiff function in C#

              Jeff,

              Because this is in my answering thread.

              I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
              code in your idea the difference between those two answers. Don't understand
              me wrong, nothing wrong with Alberto its code, however you give the
              impression there is something wrong with mine?

              Cor

              "Jeff Johnson" <i.get@enough.s pamschreef in bericht
              news:eicRybLJJH A.1968@TK2MSFTN GP06.phx.gbl...
              "andyoye" <andyoye@nospam .comwrote in message
              news:ekPoOSLJJH A.5696@TK2MSFTN GP06.phx.gbl...
              >
              >startDate field value is 10-01-09
              >endDate field value is 10-05-09
              >>
              >How can I get 4 .....(endDate-startDate)
              >
              Alberto gave you the answer.
              >

              Comment

              • andyoye

                #8
                Re: Use VB DateDiff function in C#

                I got it guys, I was passing a wrong var

                thanks all.. you all rocks!!!


                "andyoye" <andyoye@nospam .comwrote in message
                news:Op2$VoLJJH A.4600@TK2MSFTN GP06.phx.gbl...
                Below is smaple code from my form
                >
                {
                >
                1: XPathNavigator docFM = this.CreateNavi gator();
                >
                2: string date_form = docFM.SelectSin gleNode("/my:myFields/my:startDate",
                this.NamespaceM anager).Value;
                >
                3: XPathNavigator docFN = this.CreateNavi gator();
                >
                4: string date_to = docFN.SelectSin gleNode("/my:myFields/my:startTime",
                this.NamespaceM anager).Value;
                >
                5: DateTime startDate = Convert.ToDateT ime(date_frorm) ;
                >
                6: DateTime endDate = Convert.ToDateT ime(date_to);
                >
                7: TimeSpan dateDifference = endDate.Subtrac t(startDate);
                >
                8: int days = dateDifference. Days;
                >
                9: string daysString = days.ToString() ;
                >
                10: XPathNavigator dateDiffTextBox Value =
                this.CreateNavi gator().SelectS ingleNode("/my:myFields/my:dateDiff",
                this.NamespaceM anager);
                >
                11: dateDiffTextBox Value.SetValue( daysString);
                >
                }
                >
                I get
                "String was not recognized as a valid DateTime." at line 6
                "When convertin a string to DateTime, parse the string to take the date
                before putting each variable into the DateTime object"
                >
                thx
                >
                >
                >
                "Jeff Johnson" <i.get@enough.s pamwrote in message
                news:eicRybLJJH A.1968@TK2MSFTN GP06.phx.gbl...
                >"andyoye" <andyoye@nospam .comwrote in message
                >news:ekPoOSLJJ HA.5696@TK2MSFT NGP06.phx.gbl.. .
                >>
                >>startDate field value is 10-01-09
                >>endDate field value is 10-05-09
                >>>
                >>How can I get 4 .....(endDate-startDate)
                >>
                >Alberto gave you the answer.
                >>
                >
                >

                Comment

                • Jeff Johnson

                  #9
                  Re: Use VB DateDiff function in C#

                  "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                  news:C4021018-66CD-4130-A1E9-4D660D0C14CD@mi crosoft.com...
                  Jeff,
                  >
                  Because this is in my answering thread.
                  >
                  I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
                  code in your idea the difference between those two answers. Don't
                  understand me wrong, nothing wrong with Alberto its code, however you give
                  the impression there is something wrong with mine?
                  Wait, are you asking me why I responded the way I did? If so, I was simply
                  telling andyoye that he was asking for an answer yet again when Alberto had
                  already given him the answer. In other words, "Pay attention: you've already
                  been told!"


                  Comment

                  • andyoye

                    #10
                    Re: Use VB DateDiff function in C#

                    I already said: You ALL Rocks, thanks.

                    How about getting minutes difference? I mod the days code

                    TimeSpan timeDifference = endTime.Subtrac t(startTime);

                    int hours = timeDifference. Hours;

                    string hoursString = hours.ToString( );

                    Now I do get hours but not minutes.



                    "Jeff Johnson" <i.get@enough.s pamwrote in message
                    news:egvb$SNJJH A.4280@TK2MSFTN GP04.phx.gbl...
                    "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                    news:C4021018-66CD-4130-A1E9-4D660D0C14CD@mi crosoft.com...
                    >
                    >Jeff,
                    >>
                    >Because this is in my answering thread.
                    >>
                    >I am curious, what is beside that I use C# 2008 code and Alberto C# 2003
                    >code in your idea the difference between those two answers. Don't
                    >understand me wrong, nothing wrong with Alberto its code, however you
                    >give the impression there is something wrong with mine?
                    >
                    Wait, are you asking me why I responded the way I did? If so, I was simply
                    telling andyoye that he was asking for an answer yet again when Alberto
                    had already given him the answer. In other words, "Pay attention: you've
                    already been told!"
                    >

                    Comment

                    • Jeff Johnson

                      #11
                      Re: Use VB DateDiff function in C#

                      "andyoye" <andyoye@nospam .comwrote in message
                      news:OZpUlqVJJH A.1156@TK2MSFTN GP05.phx.gbl...
                      >I already said: You ALL Rocks, thanks.
                      >
                      How about getting minutes difference? I mod the days code
                      >
                      TimeSpan timeDifference = endTime.Subtrac t(startTime);
                      >
                      int hours = timeDifference. Hours;
                      >
                      string hoursString = hours.ToString( );
                      >
                      Now I do get hours but not minutes.
                      Okay, what is the ULTIMATE result you're looking for? In other words, given
                      two DateTime values, what would you like to see as the result of the
                      difference between them?


                      Comment

                      • andyoye

                        #12
                        Re: Use VB DateDiff function in C#

                        how about time difference to the minutes? below date and time values are
                        entered by users in text fields.

                        So startDate = 10-01-2008 startEnd= 2:05 PM
                        endDate = 10-03-2008 endTime = 1:00 PM

                        result should be 1days 22 hrs 55mins


                        "Jeff Johnson" <i.get@enough.s pamwrote in message
                        news:OxWMYCYJJH A.4672@TK2MSFTN GP05.phx.gbl...
                        "andyoye" <andyoye@nospam .comwrote in message
                        news:OZpUlqVJJH A.1156@TK2MSFTN GP05.phx.gbl...
                        >
                        >>I already said: You ALL Rocks, thanks.
                        >>
                        >How about getting minutes difference? I mod the days code
                        >>
                        >TimeSpan timeDifference = endTime.Subtrac t(startTime);
                        >>
                        >int hours = timeDifference. Hours;
                        >>
                        >string hoursString = hours.ToString( );
                        >>
                        >Now I do get hours but not minutes.
                        >
                        Okay, what is the ULTIMATE result you're looking for? In other words,
                        given two DateTime values, what would you like to see as the result of the
                        difference between them?
                        >

                        Comment

                        • Jeff Johnson

                          #13
                          Re: Use VB DateDiff function in C#

                          "andyoye" <andyoye@nospam .comwrote in message
                          news:eDYM3k6JJH A.1556@TK2MSFTN GP03.phx.gbl...
                          how about time difference to the minutes? below date and time values are
                          entered by users in text fields.
                          >
                          So startDate = 10-01-2008 startEnd= 2:05 PM
                          endDate = 10-03-2008 endTime = 1:00 PM
                          >
                          result should be 1days 22 hrs 55mins
                          [Sorry for the late reply; I forgot I started following this group!]

                          The best thing to do is to get the difference between the dates in the
                          smallest possible unit you're looking for (minutes in this case) and then
                          apply math to extract the larger units. For example, you've got 2815 minutes
                          there. You make some constants like

                          private const int MINUTES_PER_DAY = 1440
                          private const int MINUTES_PER_HOU R = 60 // Yes, this one might be
                          overkill....

                          and then you test your value against them. If you have more than the
                          constant, then you've got at least one of that unit, so you do some integer
                          division to get the number of units and then you test the remainder against
                          the next smaller unit.

                          Instead of constants, you could make a class which holds the number and a
                          description (1440 / "days", 60 / "hrs", etc.) and then make an array or
                          collection of objects of that class so you could process them in a loop.
                          Then you could make different arrays/collections for different units
                          (seconds, milliseconds, etc.).


                          Comment

                          Working...