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. checking time values

checking time values

Scheduled Pinned Locked Moved C#
question
2 Posts 2 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.
  • D Offline
    D Offline
    draco_iii
    wrote on last edited by
    #1

    I have three time values and they are in string format. string time1 = "12:00:00" string time2 = "12:30:00" string time3 = "13:00:00" time2 changes, but time1 and time3 stay the same How can I check to see if time2 is between time1 and time3? I have thought of several different ways but they are all lengthy and involved. I though maybe something like this would work but want to make sure it will always work. if(time1 <= time2 <= time3) { do such -n- such } will this work for strings

    R 1 Reply Last reply
    0
    • D draco_iii

      I have three time values and they are in string format. string time1 = "12:00:00" string time2 = "12:30:00" string time3 = "13:00:00" time2 changes, but time1 and time3 stay the same How can I check to see if time2 is between time1 and time3? I have thought of several different ways but they are all lengthy and involved. I though maybe something like this would work but want to make sure it will always work. if(time1 <= time2 <= time3) { do such -n- such } will this work for strings

      R Offline
      R Offline
      Rocky Moore
      wrote on last edited by
      #2

      draco_iii wrote: if(time1 <= time2 <= time3) { The comparison of time1 <= time 2 returns a bool (if a comparable type, more on that shortly) which it would then compare that bool value to time3. It would be

      time2 >= time1 && time2 <= time3

      But there is another problem, you cannot use that as comparisons for strings. You would use the string.CompareTo() with returns a int value: < 0 : string less than target 0 : same > 0 : target less than string So you would have:

      if( time2.CompareTo(time1) >= 0 && time2.CompareTo(time3) <= 0)
      {
      do...
      }

      Rocky Moore <><

      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