473,503 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Apr 7 '07 #1
10 2530
"Andrew" <An****@somewhereoutthere.comwrote in message
news:uH**************@TK2MSFTNGP03.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?

Apr 8 '07 #2
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" <st*****@hotmail.comschreef in bericht
news:At**************@newsread3.news.pas.earthlink .net...
"Andrew" <An****@somewhereoutthere.comwrote in message
news:uH**************@TK2MSFTNGP03.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?

Apr 8 '07 #3
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
Apr 8 '07 #4
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

Apr 9 '07 #5
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:O7**************@TK2MSFTNGP04.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?

Apr 9 '07 #6
Peter wrote:
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:O7**************@TK2MSFTNGP04.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
Apr 9 '07 #7
"Matthias Tacke" <Ma******@Tacke.dewrote in message
news:ev**********@news.albasani.net...
Peter wrote:
>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:O7**************@TK2MSFTNGP04.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...

Apr 9 '07 #8
Thank you Charlie, very much appreciated.

-- Andrew
"Charlie Brown" <cb****@duclaw.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.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="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

Apr 13 '07 #9
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" <st*****@hotmail.comwrote in message
news:yd*********************@newsread2.news.pas.ea rthlink.net...
"Matthias Tacke" <Ma******@Tacke.dewrote in message
news:ev**********@news.albasani.net...
>Peter wrote:
>>"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:O7**************@TK2MSFTNGP04.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...

Apr 13 '07 #10
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
Apr 14 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2056
by: Robert Mark Bram | last post by:
Howdy All! I am trying to write a very brief comparison of the Date and Math objects in terms of instance v static objects. What I have below is my best so far. Any criticisms or suggestions are...
2
10186
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
4
5342
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
7
2151
by: Esa | last post by:
function Validaattori_1viikko(source,args) { var star_tdate; var end_date; start_date = document.all("Laina_alku").value; end_date = document.all("Laina_loppu").value; var start_day; var...
6
4824
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By "common" I mean at the least ones described in this...
2
2442
by: Riegn Man | last post by:
I have a problem with access and our time clocks. We have time clocks that put out a .log file with the badge swipes for everybody. There is one .log file for each day. I am pulling that data...
3
29012
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the...
1
4526
by: Sam | last post by:
How do I convert Julian Date to Calendar Date in ASP.Net 1.1 based on following guideline found at Internet? To convert Julian date to Gregorian date: double JD = 2299160.5; double Z =...
12
1663
by: Woody Splawn | last post by:
I am trying to determine the age of a person based on two dates, the Date of Birth and Today(). I have a function that does this but it is kludgey and is giving me an age that is pretty close...
4
15666
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.