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. Comparing Times

Comparing Times

Scheduled Pinned Locked Moved C#
question
5 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.
  • A Offline
    A Offline
    AmbiguousName
    wrote on last edited by
    #1

    hello guys... I was thinking if there is any way to compare two times which are in different format. Like if I have these times

    time1 = "01/12/2005";
    time2 = "Dec 1, 2005";
    //OR: time2 = "Dec 1, 05"
    //OR: time2 = "December 1, 2005"
    //OR: time2 = "12/01/2005"
    //OR: all possible ways for time2

    Is it possible to compare these two times? Thanks

    This world is going to explode due to international politics, SOON.

    OriginalGriffO A A 3 Replies Last reply
    0
    • A AmbiguousName

      hello guys... I was thinking if there is any way to compare two times which are in different format. Like if I have these times

      time1 = "01/12/2005";
      time2 = "Dec 1, 2005";
      //OR: time2 = "Dec 1, 05"
      //OR: time2 = "December 1, 2005"
      //OR: time2 = "12/01/2005"
      //OR: all possible ways for time2

      Is it possible to compare these two times? Thanks

      This world is going to explode due to international politics, SOON.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Yes. Never ever, ever try to compare dates, or any other numbers as strings: convert them to an appropriate class first. In this case, that class is DateTime:

      DateTime dt1 = DateTime.Parse("01/12/2005");
      DateTime dt2 = DateTime.Parse("Dec 12 2005");
      TimeSpan diff = dt2 - dt1;

      You may need to use ParseExact, or TryParseExact if you know what format the date will be in to avoid problems like 03/04/05 - is that 3rd Apr 2005, or 4th March 2005, or 5th Apr 2003?

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 1 Reply Last reply
      0
      • A AmbiguousName

        hello guys... I was thinking if there is any way to compare two times which are in different format. Like if I have these times

        time1 = "01/12/2005";
        time2 = "Dec 1, 2005";
        //OR: time2 = "Dec 1, 05"
        //OR: time2 = "December 1, 2005"
        //OR: time2 = "12/01/2005"
        //OR: all possible ways for time2

        Is it possible to compare these two times? Thanks

        This world is going to explode due to international politics, SOON.

        A Offline
        A Offline
        Apocalypse Now
        wrote on last edited by
        #3

        DateTime time1 = DateTime.Parse("01/12/2005"); DateTime time2 = DateTime.Parse("Dec 1, 2005"); if (time1==time2) {... } else { ... }

        1 Reply Last reply
        0
        • A AmbiguousName

          hello guys... I was thinking if there is any way to compare two times which are in different format. Like if I have these times

          time1 = "01/12/2005";
          time2 = "Dec 1, 2005";
          //OR: time2 = "Dec 1, 05"
          //OR: time2 = "December 1, 2005"
          //OR: time2 = "12/01/2005"
          //OR: all possible ways for time2

          Is it possible to compare these two times? Thanks

          This world is going to explode due to international politics, SOON.

          A Offline
          A Offline
          AmitGajjar
          wrote on last edited by
          #4

          Hi, You can use Parse,TryParse,TryParseExact to parse your string to convert it into DateTime object. each have their own pros/cons. Here is the sample code for finding difference between dates.

          DateTime dt1 = DateTime.Parse("firstDate");
          DateTime dt2 = DateTime.Parse("secondDate");
          TimeSpan dateDifference = dt1.Subtract(dt2);

          -Amit

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Yes. Never ever, ever try to compare dates, or any other numbers as strings: convert them to an appropriate class first. In this case, that class is DateTime:

            DateTime dt1 = DateTime.Parse("01/12/2005");
            DateTime dt2 = DateTime.Parse("Dec 12 2005");
            TimeSpan diff = dt2 - dt1;

            You may need to use ParseExact, or TryParseExact if you know what format the date will be in to avoid problems like 03/04/05 - is that 3rd Apr 2005, or 4th March 2005, or 5th Apr 2003?

            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

            A Offline
            A Offline
            AmitGajjar
            wrote on last edited by
            #5

            Hey, i didn't seen your full reply. although both the code are same :)

            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