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. compare 2 datetime and get hours

compare 2 datetime and get hours

Scheduled Pinned Locked Moved C#
helptutorial
3 Posts 3 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.
  • M Offline
    M Offline
    Mohammed Elkholy
    wrote on last edited by
    #1

    first thanks for evryone help others in this website i would like to compare between tow datetime and get the deffrance between them in hours example in parking we get the entrance datetime and leave datetime i want to compare tow datetime and get the real hours of parking

    MD_NADA

    J J 2 Replies Last reply
    0
    • M Mohammed Elkholy

      first thanks for evryone help others in this website i would like to compare between tow datetime and get the deffrance between them in hours example in parking we get the entrance datetime and leave datetime i want to compare tow datetime and get the real hours of parking

      MD_NADA

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

      TimeSpan ts = dateTime1 - dateTime2;
      Console.WriteLine(ts.TotalHours);

      1 Reply Last reply
      0
      • M Mohammed Elkholy

        first thanks for evryone help others in this website i would like to compare between tow datetime and get the deffrance between them in hours example in parking we get the entrance datetime and leave datetime i want to compare tow datetime and get the real hours of parking

        MD_NADA

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

        You should look into the TimeSpan object.

        TimeSpan timeSpan = DateTime.Now.Subtract(new DateTime(2008, 10, 3));

        To get the whole number of hours, its as simple as int hours = timeSpan.Hours If you want to get the total hours, double totalHours = timeSpan.TotalHours If you're unclear on any of this I recommend taking a look at the documentation or doing a google search on System.TimeSpan

        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