Date math

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew

    #1

    Date math

    Having trouble with some date computations. I need to get the date of a
    specific day within a month - the 1st Thursday or the 2nd Monday, etc. I
    found an article ( http://www.dotnetspider.com/kb/Article1252.aspx) that
    gives examples using on the DateAdd() and DateDiff() functions, but it seems
    that not all of the examples work as described.... and of course, the last
    example in the article is very close to what I need yet does not work (I
    keep getting 08/11/0287 when I run it).

    My goal is to have a way I can tell if it is a specific day of the month so
    I can display a message to the user. I am just not able to get my head
    around the necessary date math.

    Any help is greatly appreciated. Thanks

    -- Andrew


  • Peter

    #2
    Re: Date math

    "Andrew" <Andrew@somewhe reoutthere.comw rote in message
    news:uHLbVQVeHH A.3956@TK2MSFTN GP03.phx.gbl...
    Having trouble with some date computations. I need to get the date of a
    specific day within a month - the 1st Thursday or the 2nd Monday, etc. I
    found an article ( http://www.dotnetspider.com/kb/Article1252.aspx) that
    gives examples using on the DateAdd() and DateDiff() functions, but it
    seems that not all of the examples work as described.... and of course,
    the last example in the article is very close to what I need yet does not
    work (I keep getting 08/11/0287 when I run it).
    >
    My goal is to have a way I can tell if it is a specific day of the month
    so I can display a message to the user. I am just not able to get my head
    around the necessary date math.
    >
    Any help is greatly appreciated. Thanks
    >
    -- Andrew
    I wrote a full-featured calendar application in PHP, and I can give you some
    conceptual ideas for the logic I used for this kind of thing:

    1) The first of any day --- Monday, Tuesday, or whatever --- in a given
    month must fall between the 1st and 7th, inclusive.

    2) Likewise, the second one must fall between the 8th and the 14th,
    inclusive. So you can just do two tests: 1) is the given date a Tuesday (or
    whatever), and 2) is it between the given date range for the week you're
    looking for?

    Example: Thanksgiving in the US falls on the fourth Thursday of November,
    which means that it must 1) be a Thursday and also 2) be in the range of
    Nov. 22-28. For a given year, there is always one and only one date that
    satisfies these criteria. So just iterate through 22-28 and see which one
    comes up as Thursday.

    3) The _last_ of a day in the month changes depending on the month... it
    must fall between the 25th and the 31st of the month (inclusive) if the
    month has 31 days, and between the 24th and the 30th (inclusive) if the
    month has 30 days. You could use this logic to figure out Memorial Day, etc.

    4) You can extend this principle to more complex patterns, such as that for
    Election Day (which is the first Tuesday after the first Monday of
    November). Using this method, simply test the range of dates Nov. 2-8 to see
    which one is a Tuesday.

    Does that give you a foothold on what you need to do?

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Date math

      Nice written Peter,

      If this is homework, then it fits exact in the place as we like to give
      information than in this newsgroup.

      Cor

      "Peter" <starrim@hotmai l.comschreef in bericht
      news:AtYRh.723$ 3P3.33@newsread 3.news.pas.eart hlink.net...
      "Andrew" <Andrew@somewhe reoutthere.comw rote in message
      news:uHLbVQVeHH A.3956@TK2MSFTN GP03.phx.gbl...
      >Having trouble with some date computations. I need to get the date of a
      >specific day within a month - the 1st Thursday or the 2nd Monday, etc. I
      >found an article ( http://www.dotnetspider.com/kb/Article1252.aspx) that
      >gives examples using on the DateAdd() and DateDiff() functions, but it
      >seems that not all of the examples work as described.... and of course,
      >the last example in the article is very close to what I need yet does not
      >work (I keep getting 08/11/0287 when I run it).
      >>
      >My goal is to have a way I can tell if it is a specific day of the month
      >so I can display a message to the user. I am just not able to get my
      >head around the necessary date math.
      >>
      >Any help is greatly appreciated. Thanks
      >>
      >-- Andrew
      >
      I wrote a full-featured calendar application in PHP, and I can give you
      some conceptual ideas for the logic I used for this kind of thing:
      >
      1) The first of any day --- Monday, Tuesday, or whatever --- in a given
      month must fall between the 1st and 7th, inclusive.
      >
      2) Likewise, the second one must fall between the 8th and the 14th,
      inclusive. So you can just do two tests: 1) is the given date a Tuesday
      (or whatever), and 2) is it between the given date range for the week
      you're looking for?
      >
      Example: Thanksgiving in the US falls on the fourth Thursday of November,
      which means that it must 1) be a Thursday and also 2) be in the range of
      Nov. 22-28. For a given year, there is always one and only one date that
      satisfies these criteria. So just iterate through 22-28 and see which one
      comes up as Thursday.
      >
      3) The _last_ of a day in the month changes depending on the month... it
      must fall between the 25th and the 31st of the month (inclusive) if the
      month has 31 days, and between the 24th and the 30th (inclusive) if the
      month has 30 days. You could use this logic to figure out Memorial Day,
      etc.
      >
      4) You can extend this principle to more complex patterns, such as that
      for Election Day (which is the first Tuesday after the first Monday of
      November). Using this method, simply test the range of dates Nov. 2-8 to
      see which one is a Tuesday.
      >
      Does that give you a foothold on what you need to do?

      Comment

      • Matthias Tacke

        #4
        Re: Date math

        Peter wrote:
        Example: Thanksgiving in the US falls on the fourth Thursday of
        November, which means that it must 1) be a Thursday and also 2) be in
        the range of Nov. 22-28. For a given year, there is always one and only
        one date that satisfies these criteria. So just iterate through 22-28
        and see which one comes up as Thursday.
        >
        3) The _last_ of a day in the month changes depending on the month... it
        must fall between the 25th and the 31st of the month (inclusive) if the
        month has 31 days, and between the 24th and the 30th (inclusive) if the
        month has 30 days. You could use this logic to figure out Memorial Day,
        etc.
        >
        4) You can extend this principle to more complex patterns, such as that
        for Election Day (which is the first Tuesday after the first Monday of
        November). Using this method, simply test the range of dates Nov. 2-8 to
        see which one is a Tuesday.
        >
        I'd always calculate the weekday number of the first of the month, using
        this as an offset. No iterating necessary then.

        Also I'd always calculate the first Monday, Tuesday.. Adding 7,14,21 for
        the second, third, fourth.

        Iterating is trying versus a bit trivial math to calculate.

        --
        Greetings
        Matthias

        Comment

        • Charlie Brown

          #5
          Re: Date math

          This should work for you. Watch the word wraps.

          ''' <summary>
          ''' Get the a date corresponding to the chosen day of the month
          ''' </summary>
          ''' <param name="firstDayO fMonth">The date of the first day of the
          month, i.e. 1/1/2000</param>
          ''' <param name="day">The day of the week you want</param>
          ''' <param name="iteration ">The occurence in the month that you
          want, i.e. 2nd Tuesday of the month = 2</param>
          ''' <returns></returns>
          ''' <remarks></remarks>
          Private Function GetDayOfMonth(B yVal firstDayOfMonth As DateTime,
          ByVal day As DayOfWeek, ByVal iteration As Integer) As DateTime
          Return DateAdd(DateInt erval.Day, (((day + 7) -
          firstDayOfMonth .DayOfWeek) Mod 7) + ((iteration - 1) * 7),
          firstDayOfMonth )
          End Function

          Comment

          • Peter

            #6
            Re: Date math

            "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
            news:O7uyivaeHH A.1868@TK2MSFTN GP04.phx.gbl...
            Nice written Peter,
            >
            If this is homework, then it fits exact in the place as we like to give
            information than in this newsgroup.
            I'm sorry... I don't quite understand your sentence?

            Comment

            • Matthias Tacke

              #7
              Re: Date math

              Peter wrote:
              "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
              news:O7uyivaeHH A.1868@TK2MSFTN GP04.phx.gbl...
              >Nice written Peter,
              >>
              >If this is homework, then it fits exact in the place as we like to
              >give information than in this newsgroup.
              >
              I'm sorry... I don't quite understand your sentence?
              You did right, to not present a ready solution, but provided information so
              the OP can do the homework himself.

              --
              Greetings
              Matthias

              Comment

              • Peter

                #8
                Re: Date math

                "Matthias Tacke" <Matthias@Tacke .dewrote in message
                news:eve5r1$sqq $1@news.albasan i.net...
                Peter wrote:
                >"Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                >news:O7uyivaeH HA.1868@TK2MSFT NGP04.phx.gbl.. .
                >>Nice written Peter,
                >>>
                >>If this is homework, then it fits exact in the place as we like to give
                >>information than in this newsgroup.
                >>
                >I'm sorry... I don't quite understand your sentence?
                >
                You did right, to not present a ready solution, but provided information
                so the OP can do the homework himself.
                Ah. Yes, that was the point...

                Comment

                • Andrew

                  #9
                  Re: Date math

                  Thank you Charlie, very much appreciated.

                  -- Andrew


                  "Charlie Brown" <cbrown@duclaw. comwrote in message
                  news:1176135441 .536675.297640@ y66g2000hsf.goo glegroups.com.. .
                  This should work for you. Watch the word wraps.
                  >
                  ''' <summary>
                  ''' Get the a date corresponding to the chosen day of the month
                  ''' </summary>
                  ''' <param name="firstDayO fMonth">The date of the first day of the
                  month, i.e. 1/1/2000</param>
                  ''' <param name="day">The day of the week you want</param>
                  ''' <param name="iteration ">The occurence in the month that you
                  want, i.e. 2nd Tuesday of the month = 2</param>
                  ''' <returns></returns>
                  ''' <remarks></remarks>
                  Private Function GetDayOfMonth(B yVal firstDayOfMonth As DateTime,
                  ByVal day As DayOfWeek, ByVal iteration As Integer) As DateTime
                  Return DateAdd(DateInt erval.Day, (((day + 7) -
                  firstDayOfMonth .DayOfWeek) Mod 7) + ((iteration - 1) * 7),
                  firstDayOfMonth )
                  End Function
                  >

                  Comment

                  • Andrew

                    #10
                    Re: Date math

                    I didn't realize I needed to post the reason behind my posting a question.
                    I'm not in school, I'm 34, trying to put together a quick webpage with a
                    calendar on it that will post a reminder on a specific day (ie: 2nd Monday,
                    3rd Thursday) rather than using a database to store the dates, which I do
                    not feel like paying the extra cost for to the company hosting the site.

                    While I appreciate you're time Peter, I posted the location and specific
                    function I was already looking at that did what I wanted, but was trying to
                    understand why I was not getting the result the author claimed.

                    I don't have a lot of time, just trying to put up a quick page, and came
                    here looking for help. I did not come here to be lectured and treated as if
                    I am some school kid trying to cheat on homework. I find that offensive and
                    insulting.

                    Good day.

                    -- Andrew



                    "Peter" <starrim@hotmai l.comwrote in message
                    news:ydzSh.1359 81$_73.122310@n ewsread2.news.p as.earthlink.ne t...
                    "Matthias Tacke" <Matthias@Tacke .dewrote in message
                    news:eve5r1$sqq $1@news.albasan i.net...
                    >Peter wrote:
                    >>"Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                    >>news:O7uyivae HHA.1868@TK2MSF TNGP04.phx.gbl. ..
                    >>>Nice written Peter,
                    >>>>
                    >>>If this is homework, then it fits exact in the place as we like to give
                    >>>informatio n than in this newsgroup.
                    >>>
                    >>I'm sorry... I don't quite understand your sentence?
                    >>
                    >You did right, to not present a ready solution, but provided information
                    >so the OP can do the homework himself.
                    >
                    Ah. Yes, that was the point...

                    Comment

                    • Cor Ligthert [MVP]

                      #11
                      Re: Date math

                      Andrew,
                      I find that offensive and insulting.
                      You are free to find that, however I don't see any insult or offensive in
                      the thread except by you.

                      If you had start giving this information, there would have been probably
                      more people to help you.
                      We try to help as well schoolkids, by not doing there home work for them and
                      I still find the way Peter gave you the information very well. What do you
                      demand, free code.

                      Than go somewhere and pay for it.

                      Just my opinion,

                      Cor


                      Comment

                      Working...