How to get Days

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pskvenkat
    New Member
    • Mar 2008
    • 10

    #1

    How to get Days

    Hi This is venkat, i am using asp.net(C#)2008 .i am the beginner for .net, i want to get the Number of days, it

    Ex
    Days= FromDate- ToDate ? how will i get answer like 3 days
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    DateTime objects can be subtracted from each other.
    The result can be cast to a TimeSpan object which contains properties for telling you the total number of days in between the two dates.

    Comment

    • kunal pawar
      Contributor
      • Oct 2007
      • 297

      #3
      try following code

      System.DateTime dtTodayNoon = new System.DateTime (2006, 9, 13, 12, 0, 0);

      System.DateTime dtTodayMidnight = new System.DateTime (2006, 9, 13, 0, 0, 0);

      System.TimeSpan diffResult = dtTodayNoon.Sub tract(dtYestMid night);

      Response.Write( "Yesterday Midnight - Today Noon = " + diffResult.Days );

      Response.Write( "Yesterday Midnight - Today Noon = " + diffResult.Tota lDays);

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        [CODE=cpp]
        //c#
        DateTime date1 = new DateTime(2008, 4, 11, 12, 0, 0);
        DateTime date2 = new DateTime(2008, 4, 14, 23, 0, 0);
        TimeSpan difference = date2.Subtract( date1);
        int days = difference.Days ;
        double fractionalDays = difference.Tota lDays;
        [/code]
        difference.Days will give you the integer number of days, difference.Tota lDays will give you a fractional number.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          TotalDays will be the one you want to use. Days is just the "days portion".
          If the difference between the two dates is 1year and 2 days, "Days" will be 2 while "TotalDays" will be like 367 or so.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Originally posted by Plater
            TotalDays will be the one you want to use. Days is just the "days portion".
            If the difference between the two dates is 1year and 2 days, "Days" will be 2 while "TotalDays" will be like 367 or so.
            I don't think so. I tested this:
            [code=cpp]
            //code is c#
            DateTime date1 = new DateTime(2006, 4, 11, 12, 0, 0);
            DateTime date2 = new DateTime(2008, 4, 14, 23, 0, 0);
            TimeSpan difference = date2.Subtract( date1);
            int days = difference.Days ;
            double totalDays = difference.Tota lDays;
            Console.WriteLi ne(days.ToStrin g() + "\n" + totalDays.ToStr ing());
            [/code]

            And the console output is:
            734
            734.45833333333 3
            I think that the only difference is the data type.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Hmm interesting.
              I had only really tested this with the time variables.
              I was trying to determine if enough seconds had passed and discovered that .Seconds would bump up .Minutes and enver go over 59, while TotalSeconds would give an accurate account.
              I had assumed the rest of the time/date variables worked the same way.
              Code:
              DateTime date1 = new DateTime(2007, 12, 11, 12, 0, 0);
              DateTime date2 = new DateTime(2008, 1, 14, 23, 0, 1);
              TimeSpan difference = (TimeSpan)(date2-date1);
              
              Console.WriteLine(difference.Seconds.ToString() + "\n" + difference.TotalSeconds.ToString());
              And the output:
              >1
              >2977201

              Comment

              Working...