473,657 Members | 2,546 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 2546
"Andrew" <An****@somewhe reoutthere.comw rote in message
news:uH******** ******@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?

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*****@hotmai l.comschreef in bericht
news:At******** ******@newsread 3.news.pas.eart hlink.net...
"Andrew" <An****@somewhe reoutthere.comw rote in message
news:uH******** ******@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?

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="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

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

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

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

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

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*****@hotmai l.comwrote in message
news:yd******** *************@n ewsread2.news.p as.earthlink.ne t...
"Matthias Tacke" <Ma******@Tacke .dewrote in message
news:ev******** **@news.albasan i.net...
>Peter wrote:
>>"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:O7****** ********@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...

Apr 13 '07 #10

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

Similar topics

1
2073
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 most welcome! Date is an instance object. You use Date by creating instances of it - you call methods on those instances. - you change data to do with each instance.
2
10203
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 document.write(mmddyy)
4
5364
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 calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
7
2165
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 end_day; var start_month; var end_month;
6
4829
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 W3C note: http://www.w3.org/TR/NOTE-datetime For my requirements the code would be need to be open sourceable under the BSD license.
2
2455
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 into an access database to manipulate it. In the table I have: FIRSTNAME | LASTNAME | BADGE# | DATE | TIME The problem is that the time clock doesn’t differentiate between a “clock in” and a “clock out”. It only records badge...
3
29034
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 week . I've used the weekday() function to convert dates to numberic days of the week (1-7) I've used cdbl (date) to convert a date to a serial number, but then I do math with the number and I can't seem to convert this back to a date.
1
4538
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 = Math.Floor(JD+0.5); double W = Math.Floor((Z - 1867216.25)/36524.25); double X = Math.Floor(W/4);
12
1681
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 but only because I have figured into the equation (manually) the approximate number of leap years. The code reads: Dim TS As New TimeSpan Dim NumDays As Integer
4
15725
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 which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e....
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.