Date math

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

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
 
Andrew said:
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?
 
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 said:
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.
 
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="firstDayOfMonth">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(ByVal firstDayOfMonth As DateTime,
ByVal day As DayOfWeek, ByVal iteration As Integer) As DateTime
Return DateAdd(DateInterval.Day, (((day + 7) -
firstDayOfMonth.DayOfWeek) Mod 7) + ((iteration - 1) * 7),
firstDayOfMonth)
End Function
 
Cor Ligthert said:
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?
 
Peter said:
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.
 
Matthias Tacke said:
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...
 
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
 
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
 
Back
Top