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. Other Discussions
  3. The Weird and The Wonderful
  4. Dating

Dating

Scheduled Pinned Locked Moved The Weird and The Wonderful
19 Posts 14 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.
  • Q QuiJohn

    Check to see if the day defined by "year", "mon" and "day" is between dtStart and dtEnd:

            if (year >= dtStart.Year && year <= dtEnd.Year &&
               mon >= dtStart.Month && mon <= dtEnd.Month &&
               day >= dtStart.Day && day <= dtEnd.Day)
            {
               ok = true;
            }
            else
            {
               ok = false;
            }
    

    :(


    He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

    M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #10

    Sadly, I've written code like that. :doh: Marc

    J 1 Reply Last reply
    0
    • L Luc Pattyn

      in order to fix the bugs, you could write:

      ok=!(year<dtStart.Year||(year==dtStart.Year&&(month<dtStart.Month||(month==dtStart.Month&&day<dtStart.Day)))||
      year>dtEnd.Year||(year==dtEnd.Year&&(month>dtEnd.Month||(month==dtEnd.Month&&day>dtEnd.Day))))

      which still sits in the right forum. :)

      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.

      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #11

      Minor correction. Try:

      ok=!(year<dtStart.Year||(year==dtStart.Year&&(month<dtStart.Month||(year==dtStart.Year&&month==dtStart.Month&&day<dtStart.Day)))||
      year>dtEnd.Year||(year==dtEnd.Year&&(month>dtEnd.Month||(year==dtStart.Year&&month==dtEnd.Month&&day>dtEnd.Day))))

      Else you'll end up with 'valid' dates that are well within the month/date bracket, but in the wrong year(s)! hth :suss:

      L 1 Reply Last reply
      0
      • S Stefan_Lang

        Minor correction. Try:

        ok=!(year<dtStart.Year||(year==dtStart.Year&&(month<dtStart.Month||(year==dtStart.Year&&month==dtStart.Month&&day<dtStart.Day)))||
        year>dtEnd.Year||(year==dtEnd.Year&&(month>dtEnd.Month||(year==dtStart.Year&&month==dtEnd.Month&&day>dtEnd.Day))))

        Else you'll end up with 'valid' dates that are well within the month/date bracket, but in the wrong year(s)! hth :suss:

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

        I disagree. :)

        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
        • Q QuiJohn

          Check to see if the day defined by "year", "mon" and "day" is between dtStart and dtEnd:

                  if (year >= dtStart.Year && year <= dtEnd.Year &&
                     mon >= dtStart.Month && mon <= dtEnd.Month &&
                     day >= dtStart.Day && day <= dtEnd.Day)
                  {
                     ok = true;
                  }
                  else
                  {
                     ok = false;
                  }
          

          :(


          He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

          J Offline
          J Offline
          johannesnestler
          wrote on last edited by
          #13

          Remembers me to some date comparisons I have done with plain C - long ago :-O (but mine were at least correct :-)) I was very happy when I had to do it the first time with C# and found out I can do it like this:

                  DateTime dtPast = new DateTime(2009, 9, 9);
                  DateTime dtNow = new DateTime(2010, 10, 10);
                  DateTime dtFuture = new DateTime(2011, 11, 11);
                  bool bIsBetween = dtNow > dtPast && dtNow < dtFuture;
          
          1 Reply Last reply
          0
          • M Marc Clifton

            Sadly, I've written code like that. :doh: Marc

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #14

            Yes, but you only do it once, and then when you realize your blunder, you promise yourself to never go there again. Right? :D

            -- Kein Mitleid Für Die Mehrheit

            M 1 Reply Last reply
            0
            • J Jorgen Sigvardsson

              Yes, but you only do it once, and then when you realize your blunder, you promise yourself to never go there again. Right? :D

              -- Kein Mitleid Für Die Mehrheit

              M Offline
              M Offline
              Marc Clifton
              wrote on last edited by
              #15

              Jörgen Sigvardsson wrote:

              , you promise yourself to never go there again. Right?

              Exactly. And generalizing the blunder, you realize how evil "if" statements actually are and carefully consider the use of them! Marc

              J 1 Reply Last reply
              0
              • M Marc Clifton

                Jörgen Sigvardsson wrote:

                , you promise yourself to never go there again. Right?

                Exactly. And generalizing the blunder, you realize how evil "if" statements actually are and carefully consider the use of them! Marc

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #16

                I stopped using branches years ago. It's my way or the highway! :D

                -- Kein Mitleid Für Die Mehrheit

                1 Reply Last reply
                0
                • L Luc Pattyn

                  in order to fix the bugs, you could write:

                  ok=!(year<dtStart.Year||(year==dtStart.Year&&(month<dtStart.Month||(month==dtStart.Month&&day<dtStart.Day)))||
                  year>dtEnd.Year||(year==dtEnd.Year&&(month>dtEnd.Month||(month==dtEnd.Month&&day>dtEnd.Day))))

                  which still sits in the right forum. :)

                  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.

                  J Offline
                  J Offline
                  jsc42
                  wrote on last edited by
                  #17

                  Or (in no particular language):

                  long ymd = (year * 12 + month) * 31 + day
                  bool ok = ymd >= (dtStart.Year * 12 + dtStart.Month) * 31 + dtStart.Day
                  && ymd <= (dtEnd.Year * 12 + dtEnd.Month) * 31 + dtEnd.Day

                  This is still in the right forum as it permits 31st Nov and 33rd Feb and 0th May, -9th day of the 17th month etc. without fixing them properly; but valid dates work. An even more horrible way with similar failings, but much faster would be (using << as a shift left logical operator and | as a bitwise OR):

                  long ymd = year << 9 | month << 5 | day
                  bool ok = ymd >= (dtStart.Year << 9 | dtStart.Month << 5 | dtStart.Day)
                  && ymd <= (dtEnd.Year << 9 | dtEnd.Month << 5 | dtEnd.Day)

                  This assumes less than 32 days per month (2^5) and less than 16 months per year (2^4). The year << 9 bits are year * 32 * 16. So 33rd Feb and -9th day of the 17th month would be somewhat disasterous.

                  L 1 Reply Last reply
                  0
                  • J jsc42

                    Or (in no particular language):

                    long ymd = (year * 12 + month) * 31 + day
                    bool ok = ymd >= (dtStart.Year * 12 + dtStart.Month) * 31 + dtStart.Day
                    && ymd <= (dtEnd.Year * 12 + dtEnd.Month) * 31 + dtEnd.Day

                    This is still in the right forum as it permits 31st Nov and 33rd Feb and 0th May, -9th day of the 17th month etc. without fixing them properly; but valid dates work. An even more horrible way with similar failings, but much faster would be (using << as a shift left logical operator and | as a bitwise OR):

                    long ymd = year << 9 | month << 5 | day
                    bool ok = ymd >= (dtStart.Year << 9 | dtStart.Month << 5 | dtStart.Day)
                    && ymd <= (dtEnd.Year << 9 | dtEnd.Month << 5 | dtEnd.Day)

                    This assumes less than 32 days per month (2^5) and less than 16 months per year (2^4). The year << 9 bits are year * 32 * 16. So 33rd Feb and -9th day of the 17th month would be somewhat disasterous.

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

                    the shifting way is what I would actually do when DateTime were not available; with a little optimization:

                    int ymd = (((year<<4)+month)<<5)+day;
                    eyc.

                    IMO it is the cheapest mapping from dates to integers that supports chronological ordering. I do add parentheses, for readability if nothing else. :)

                    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
                    • L Luc Pattyn

                      in order to fix the bugs, you could write:

                      ok=!(year<dtStart.Year||(year==dtStart.Year&&(month<dtStart.Month||(month==dtStart.Month&&day<dtStart.Day)))||
                      year>dtEnd.Year||(year==dtEnd.Year&&(month>dtEnd.Month||(month==dtEnd.Month&&day>dtEnd.Day))))

                      which still sits in the right forum. :)

                      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.

                      P Offline
                      P Offline
                      Prerak Patel
                      wrote on last edited by
                      #19

                      DateTime dt = new DateTime(year, month, day);
                      return (dt <= dtEnd & dt >= dtStart);

                      How about this one? Enclosing it in try catch will cop with invalid dates too.

                      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