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 Difference Calculation In C#

Time Difference Calculation In C#

Scheduled Pinned Locked Moved C#
helpcsharptutorial
9 Posts 6 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.
  • S Offline
    S Offline
    Sanket Patil
    wrote on last edited by
    #1

    Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.

    .

    P A M J L 5 Replies Last reply
    0
    • S Sanket Patil

      Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.

      .

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      Too easy. Look at the DateTime and TimeSpan classes. Use something like

      DateTime start = new DateTime(... ... ...);
      DateTime finish = new DateTime(... ... ...);
      TimeSpan worked = finish - start;

      then pull worked apart as you need.

      Software rusts. Simon Stephenson, ca 1994.

      S 1 Reply Last reply
      0
      • P Peter_in_2780

        Too easy. Look at the DateTime and TimeSpan classes. Use something like

        DateTime start = new DateTime(... ... ...);
        DateTime finish = new DateTime(... ... ...);
        TimeSpan worked = finish - start;

        then pull worked apart as you need.

        Software rusts. Simon Stephenson, ca 1994.

        S Offline
        S Offline
        Sanket Patil
        wrote on last edited by
        #3

        Hi Perter_in_2780, Thanks for Your Reply. But Still I am getting Incorrect Result. I have only Time. When I convert the Out Time in datetime Then Both the Time that is employees out time and shift out time has default date as todays date and if the Emd Time Of Employee is 12:00 AM and Shift Out Time ois 6:00 PM Then Iam Getting the Result is 17.00 in TimeSpan. Which I dont want. I want Result as 6:00. Iam unable to calculate time difference in case when time is from PM to AM. Thanks & Regards Sanket Patil

        .

        P 1 Reply Last reply
        0
        • S Sanket Patil

          Hi Perter_in_2780, Thanks for Your Reply. But Still I am getting Incorrect Result. I have only Time. When I convert the Out Time in datetime Then Both the Time that is employees out time and shift out time has default date as todays date and if the Emd Time Of Employee is 12:00 AM and Shift Out Time ois 6:00 PM Then Iam Getting the Result is 17.00 in TimeSpan. Which I dont want. I want Result as 6:00. Iam unable to calculate time difference in case when time is from PM to AM. Thanks & Regards Sanket Patil

          .

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          You need to include at least part of the date in start and end DateTime's. The dates can be "made up", like this:

          DateTime start = new DateTime(2000, 1, 1, 18, 0, 0); //6pm "yesterday"
          DateTime finish = new DateTime(2000, 1, 2, 9, 0, 0); //9am "today"
          TimeSpan worked = finish - start;
          ...

          That should come out at 15 hours. Peter

          Software rusts. Simon Stephenson, ca 1994.

          1 Reply Last reply
          0
          • S Sanket Patil

            Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.

            .

            A Offline
            A Offline
            Abhinav S
            wrote on last edited by
            #5

            Check this[^] out.

            The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

            S 1 Reply Last reply
            0
            • A Abhinav S

              Check this[^] out.

              The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

              S Offline
              S Offline
              Sanket Patil
              wrote on last edited by
              #6

              Hi Abhinav s, Thanks for your reply But I am Not Getting The Solution if its night shift. Thanks And Regards Sanket Patil

              .

              1 Reply Last reply
              0
              • S Sanket Patil

                Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.

                .

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                For the night shift split the calc in 2 start to 24:00, 00:00 to finish. Decide which calc to use based on the start time or if the end time is less than the start time. Os as others have suggested add the date to the start/end variables. Bloody hell this is simple stuff, try thinking.

                Never underestimate the power of human stupidity RAH

                1 Reply Last reply
                0
                • S Sanket Patil

                  Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.

                  .

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  This is exactly why you store start and end date and not just time. If you stored: start date = 2010-09-12 20:00:00 end date = 2010-09-13 07:00:00 then you would have no problem. Go back and fix your model/database before you continue with incorrect data storage.

                  1 Reply Last reply
                  0
                  • S Sanket Patil

                    Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.

                    .

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi, there are only two cases that could work well: 1. you have date AND time information, not just time. Then a TimeSpan calculated as the difference of two DateTime values is perfect. 2. you only have time information, but you also know the context enforces something. In your case it would be: the time worked is anywhere between 0 and 24 hours. So, calculate the difference in any way you see fit (even with TimeSpan=DateTime-DateTime where both DateTimes have the same or no date), then enforce your boundary conditions: if the TimeSpan is less than zero, add 24 hours. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    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