Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Time Calculation

Time Calculation

Scheduled Pinned Locked Moved C#
tutorial
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian22
    wrote on last edited by
    #1

    Hai! How to Calculate Total Elapsed time from given starting value and ending value when the the given values are greater than 24(ie Meter Readings)(for eg:Starting time=1015:35,Ending time=1058:15) Plz Send any Ideas, Thank You

    M 0 2 Replies Last reply
    0
    • I indian22

      Hai! How to Calculate Total Elapsed time from given starting value and ending value when the the given values are greater than 24(ie Meter Readings)(for eg:Starting time=1015:35,Ending time=1058:15) Plz Send any Ideas, Thank You

      M Offline
      M Offline
      mrcooll
      wrote on last edited by
      #2

      what do u mean by 1015:35 is it H:M if it's TimeSpan end = new TimeSpan(1015, 35, 0); TimeSpan start = new TimeSpan(1058, 15, 0); TimeSpan sub= end.Subtract(start); double TotalSeconds = sub.TotalSeconds;

      I 1 Reply Last reply
      0
      • I indian22

        Hai! How to Calculate Total Elapsed time from given starting value and ending value when the the given values are greater than 24(ie Meter Readings)(for eg:Starting time=1015:35,Ending time=1058:15) Plz Send any Ideas, Thank You

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #3

        If you convert those strings to DateTime structures (possibly using DateTime.ParseExact, I don't know the exact form of the strings you gave) then you can simply subtract the starting time from the ending time to get the elapsed time as an instance of the TimeSpan structure. That lets you access such properties as TotalHours, TotalMinutes, TotalSeconds and TotalMilliseconds

        1 Reply Last reply
        0
        • M mrcooll

          what do u mean by 1015:35 is it H:M if it's TimeSpan end = new TimeSpan(1015, 35, 0); TimeSpan start = new TimeSpan(1058, 15, 0); TimeSpan sub= end.Subtract(start); double TotalSeconds = sub.TotalSeconds;

          I Offline
          I Offline
          indian22
          wrote on last edited by
          #4
               DateTime date1 = System.Convert.ToDateTime(txtstarttime.Text);
          
              DateTime date2 = System.Convert.ToDateTime(txtendtime.Text);
              TimeSpan ts = new TimeSpan();
          
              ts = date2.Subtract(date1);
              DateTime dt = DateTime.MinValue.Add(ts);
               txttottime.Text=ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds  ;
          

          I have used this but it Produce error when the given values are greater than 24

          0 M 2 Replies Last reply
          0
          • I indian22
                 DateTime date1 = System.Convert.ToDateTime(txtstarttime.Text);
            
                DateTime date2 = System.Convert.ToDateTime(txtendtime.Text);
                TimeSpan ts = new TimeSpan();
            
                ts = date2.Subtract(date1);
                DateTime dt = DateTime.MinValue.Add(ts);
                 txttottime.Text=ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds  ;
            

            I have used this but it Produce error when the given values are greater than 24

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #5

            What format are txtstarttime.Text and txtendtime.Text in? Are they hours:minutes:seconds like your output? If so, then you can replace the System.Convert.ToDateTime calls with System.DateTime.Parse("input goes here", "hh:mm:ss", null)

            I 1 Reply Last reply
            0
            • 0 0x3c0

              What format are txtstarttime.Text and txtendtime.Text in? Are they hours:minutes:seconds like your output? If so, then you can replace the System.Convert.ToDateTime calls with System.DateTime.Parse("input goes here", "hh:mm:ss", null)

              I Offline
              I Offline
              indian22
              wrote on last edited by
              #6

              My Format is ("HH:MM")

              0 A 2 Replies Last reply
              0
              • I indian22

                My Format is ("HH:MM")

                0 Offline
                0 Offline
                0x3c0
                wrote on last edited by
                #7

                You can replace the System.Convert.ToDateTime calls with System.DateTime.Parse("input goes here", "hh:mm", null). The first parameter will be your Text properties instead of the string "input goes here"

                1 Reply Last reply
                0
                • I indian22
                       DateTime date1 = System.Convert.ToDateTime(txtstarttime.Text);
                  
                      DateTime date2 = System.Convert.ToDateTime(txtendtime.Text);
                      TimeSpan ts = new TimeSpan();
                  
                      ts = date2.Subtract(date1);
                      DateTime dt = DateTime.MinValue.Add(ts);
                       txttottime.Text=ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds  ;
                  

                  I have used this but it Produce error when the given values are greater than 24

                  M Offline
                  M Offline
                  mrcooll
                  wrote on last edited by
                  #8

                  u can use this but i don't recommend it string x1="1015:35"; string[] r=x1.Split(':'); TimeSpan end = new TimeSpan(int.Parse(r[0]), int.Parse(r[1]), 0); //bla bla bla //bla bla bla TimeSpan sub= end.Subtract(start);

                  1 Reply Last reply
                  0
                  • I indian22

                    My Format is ("HH:MM")

                    A Offline
                    A Offline
                    Alan N
                    wrote on last edited by
                    #9

                    Hi, Hmm, you need to stop for a moment and think about what you are trying to do. If the measured time span is 24 hours or more then information about the number of days that have passed is required to perform the calculation. If the time span is guaranteed to be less than 24 hours the following pseudocode is valid

                    // Calculate time span
                    duration = endtime - starttime;
                    // if the value is negative then assume that
                    // midnight has passed
                    // and therefore end day = start day + 1
                    if duration < 0 then duration = duration + 24 hours;

                    Note how this would fail as soon as the actual time span is 24 hours or more. Alan. [EDIT corrected error in code fragment]

                    modified on Sunday, May 31, 2009 9:29 AM

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups