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. DateTime, DayOfWeek in range comparison

DateTime, DayOfWeek in range comparison

Scheduled Pinned Locked Moved C#
helptutorialquestion
19 Posts 5 Posters 2 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.
  • P PIEBALDconsult

    Huh, interesting. I wonder how it performs in a loop.

    realJSOPR Offline
    realJSOPR Offline
    realJSOP
    wrote on last edited by
    #10

    If performance is an issue (in a loop), you could always wait until the loop is done and reset to the proper 1st day at that point, thus saving n calls to do it in that checkpoint function.

    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

    1 Reply Last reply
    0
    • B buchstaben

      Hello, I'm stuck with the following issue: let's say there is a DateTime 'timepoint', wich is passed in my method. I need to check now, whether timepoint's DayOfWeek is between 'fromDayOfWeek' and 'toDayOfWeek'. that's what I've done so far:

      protected override bool checkTimepoint(DateTime timepoint, DayOfWeek from, DayOfWeek to)
      {
      bool retValue;

              retValue = timepoint.DayOfWeek >= from && timepoint.DayOfWeek <= to;
      
              return retValue;
          }
      

      This will work for ranges like Monday-Friday, Thursday-Wednesday, Wednesday-Wednesday, but it will fail for something like Saturday-Sunday, since Sunday (0) is lower than Saturday(6). Any idea on how to logically implement this issue? Thanks in advance.

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #11

      Ignore my first post. This is the best way (notice no bizarre culture crap going on):

      DayOfWeek from = DayOfWeek.Thursday;
      DayOfWeek to = DayOfWeek.Tuesday;

      DayOfWeek currentDay = DayOfWeek.Wednesday;

      bool inRange = (currentDay >= from || currentDay <= to);

      Notice that I'm using OR instead of AND. If you make currentDay Wednesday, inRange will be false. If you make it any other day, it will be true. It's kind of pointless to do a Sunday-Saturday (or similar) comparison because any date will fall into the range. BTW, the comparison above also works for from=Saturday and to=Sunday.

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      P 2 Replies Last reply
      0
      • P PIEBALDconsult

        Huh, interesting. I wonder how it performs in a loop.

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #12

        Just thought I'd let you know I was completely wrong. :) I posted a new response to the OP with the correct solution.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        P 1 Reply Last reply
        0
        • realJSOPR realJSOP

          Just thought I'd let you know I was completely wrong. :) I posted a new response to the OP with the correct solution.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #13

          What? Say it ain't so! :omg:

          1 Reply Last reply
          0
          • realJSOPR realJSOP

            Ignore my first post. This is the best way (notice no bizarre culture crap going on):

            DayOfWeek from = DayOfWeek.Thursday;
            DayOfWeek to = DayOfWeek.Tuesday;

            DayOfWeek currentDay = DayOfWeek.Wednesday;

            bool inRange = (currentDay >= from || currentDay <= to);

            Notice that I'm using OR instead of AND. If you make currentDay Wednesday, inRange will be false. If you make it any other day, it will be true. It's kind of pointless to do a Sunday-Saturday (or similar) comparison because any date will fall into the range. BTW, the comparison above also works for from=Saturday and to=Sunday.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #14

            I was just reviewing a method I wrote a few months back where I was checking to see if a value (integer) is between two others. I decided I needed more than a true/false, there are (currently) four result values:

                \[System.FlagsAttribute()\]
                public enum CheckRangeResult
                {
                    WithinStrictRange = 0
                ,
                    Low               = 1
                ,
                    High              = 2
                ,
                    WithinLooseRange  = 3
                }
            

            Basically; given Min, Max, and Test we expect Min to be less than Max; and Test will be Lower than Min (1), Higher than Max (2), or WithinStrictRange (0). But, when Min is greater than Max and Test is between Min and Max, both tests are true, yielding WithinLooseRange (3). In the case presented here, when Min is greater than Max and Test is not between the values, that should translate to "true", but that's not currently supported. I think I need to add value 4 to indicate that Min is greater than Max, then WithinLooseRange would have value 7. I'll think about it over the weekend.

            1 Reply Last reply
            0
            • realJSOPR realJSOP

              Ignore my first post. This is the best way (notice no bizarre culture crap going on):

              DayOfWeek from = DayOfWeek.Thursday;
              DayOfWeek to = DayOfWeek.Tuesday;

              DayOfWeek currentDay = DayOfWeek.Wednesday;

              bool inRange = (currentDay >= from || currentDay <= to);

              Notice that I'm using OR instead of AND. If you make currentDay Wednesday, inRange will be false. If you make it any other day, it will be true. It's kind of pointless to do a Sunday-Saturday (or similar) comparison because any date will fall into the range. BTW, the comparison above also works for from=Saturday and to=Sunday.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #15

              It occurred to me late today that this might make a good Friday Programming Quiz. :-D

              realJSOPR 1 Reply Last reply
              0
              • P PIEBALDconsult

                It occurred to me late today that this might make a good Friday Programming Quiz. :-D

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #16

                Maybe even two questions... 1) Given the following:

                DayOfWeek startDay = DayOfWeek.Thursday;
                DayOfWeek endDay = DayOfWeek.Monday;
                DayOfWeek currentDay = DayOfWeek.Wednesday;

                Write a C# publicly accessible function that determines whether or not the day falls between startDay and endDay. The function should work for any combination of startDay/endDay values, and should return a boolean value (true if in range, false if not in range). Answer:

                public bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
                {
                return (currentDay >= startDay || currentDay <= endDay);
                }

                1. Given the function above, change the function so that the class encapsulating it does not have to be instantiated in order to use the function. Answer:

                public static bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
                {
                return (currentDay >= startDay || currentDay <= endDay);
                }

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                P 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  Maybe even two questions... 1) Given the following:

                  DayOfWeek startDay = DayOfWeek.Thursday;
                  DayOfWeek endDay = DayOfWeek.Monday;
                  DayOfWeek currentDay = DayOfWeek.Wednesday;

                  Write a C# publicly accessible function that determines whether or not the day falls between startDay and endDay. The function should work for any combination of startDay/endDay values, and should return a boolean value (true if in range, false if not in range). Answer:

                  public bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
                  {
                  return (currentDay >= startDay || currentDay <= endDay);
                  }

                  1. Given the function above, change the function so that the class encapsulating it does not have to be instantiated in order to use the function. Answer:

                  public static bool DateInRange(DayOfWeek currentDay, DayOfWeek startDay, DayofWeek endDay)
                  {
                  return (currentDay >= startDay || currentDay <= endDay);
                  }

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #17

                  I don't think that works with the "normal" case where startDay < endDay. It's also not a very general solution (which is no crime). Given a number (value) line, two points on that line that define a range, and another point to test inclusion. Normal ........... 1 > 2 < 3 Inverted (endponts not in normal order) ........... 4 < 5 > 6 The test value could be in any of the spots labeled 1 - 6. We then need to assign true/false to these spots. Clearly 2 yields true, while 1 and 3 yield false. The question is about the inverted range; The OP needs to assign true to 4 and 6, false to 5. The code I wrote in November was designed to allow assigning true to 5, false to 4 and 6. I have now modified my helper method (and enum) so it returns one of six values (it was four previously). The application then needs to provide a method that handles the translation to true/false.

                  realJSOPR 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    I don't think that works with the "normal" case where startDay < endDay. It's also not a very general solution (which is no crime). Given a number (value) line, two points on that line that define a range, and another point to test inclusion. Normal ........... 1 > 2 < 3 Inverted (endponts not in normal order) ........... 4 < 5 > 6 The test value could be in any of the spots labeled 1 - 6. We then need to assign true/false to these spots. Clearly 2 yields true, while 1 and 3 yield false. The question is about the inverted range; The OP needs to assign true to 4 and 6, false to 5. The code I wrote in November was designed to allow assigning true to 5, false to 4 and 6. I have now modified my helper method (and enum) so it returns one of six values (it was four previously). The application then needs to provide a method that handles the translation to true/false.

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #18

                    I think this should cover it then... :)

                    public static void DayInRange(DayOfWeek currentDay, DayOfWeek startDay, DayOfWeek endDay)
                    {
                    if (startDay == endDay)
                    {
                    return true;
                    }
                    if (startDay < endDay)
                    {
                    return (currentDay >= startDay && currentDay <= endDay);
                    }
                    return (currentDay >= startDay || currentDay <= endDay);
                    }

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    P 1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      I think this should cover it then... :)

                      public static void DayInRange(DayOfWeek currentDay, DayOfWeek startDay, DayOfWeek endDay)
                      {
                      if (startDay == endDay)
                      {
                      return true;
                      }
                      if (startDay < endDay)
                      {
                      return (currentDay >= startDay && currentDay <= endDay);
                      }
                      return (currentDay >= startDay || currentDay <= endDay);
                      }

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #19

                      That hurts my eyes. (Not that I should talk. :-O )

                      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