Day duration calculation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nzsquall
    New Member
    • Oct 2009
    • 22

    Day duration calculation

    Hi, I am a C# beginner, can somebody tells me how to code in the value-changed event of the DateTimePicker control so that it can accept two dates and calculates the difference between them? For example, the duration day between now and any day changed in the daytimepicker?

    Thanks in advance
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Google julian day + date.
    Its relatively straight forward to write a function to convert a Gregorian date to a Julian date. The period between the two Julian dates and fractions of days constitutes the rquired period to a precision of seconds if needed.

    Comment

    • Nzsquall
      New Member
      • Oct 2009
      • 22

      #3
      Thanks. But I am thinking of writing this code to see whether it works

      Code:
      private void dateTimePicker_ValueChanged(object sender, EventArgs e)
              {
                
                  int firstMeetingDate = int.Parse(dateTimePicker.Text);
      
                  //how can I capture the system current time as an integer?
                  //and the requirement said that I should return the int value
                  //as day, the format in my daytimePicker is ("week,dd,mm,year)
                  //how can I just capture the day and calculate the duration between 
                  //them?       
              }
      Many thanks

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Originally posted by Nzsquall
        //how can I capture the system current time as an integer?
        I think...

        Code:
        DateTime.Now.Ticks
        ... is what you're looking for.

        The DateTime class has lots of stuff to do with time. Here's the MSDN page...
        Represents an instant in time, typically expressed as a date and time of day.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          A DateTimePicker can only contain a single datetime value at a time, however you can keep track of them.
          Use the .Value property of it, rather the the .Text. This will return a DateTime object that you can use to caompare against DateTime.Now

          Comment

          • Nzsquall
            New Member
            • Oct 2009
            • 22

            #6
            Hi, I have try
            private void dateTimePicker_ ValueChanged(ob ject sender, EventArgs e)
            {
            DateTime fistMeetingDate = dateTimePicker. Value;
            DateTime currentDate = DateTime.Now;
            TimeSpan duration= currentDate - fistMeetingDate ;
            durationTextBox .Text = duration.ToStri ng();
            }
            But it appear a very strange String, for instance, today is "Tuesday, 6 October 2009", then I pick firstMeetingDat e in the dateTimePicker to "Tuesday, 1 October 2009", the result in the durationTextBox is "5.00:00:57.926 9310". How can I get a int day value?
            Thanks in advance.

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Parse the string and extract the first digit/s before the dot
              The string format you show is day.hr:m:s.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Make it say duration.TotalD ays.ToString()
                That will give you the timespan in terms of whole and fractional days (so 36 hours would give a value of 1.5 days)

                Remember to check MSDN on the objects if you are having troubles, its a good resource

                Comment

                Working...