469,887 Members | 1,263 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

Counting the Number of dayofweek in a month

Hi,
I am trying to calculate the number of DayOfWeek in a month.. for instance..
this month April I want to know how many tuesdays there are in this month,
this year
I try to use
where intDOW = FirstDayOfWeek.Tuesday
dtmFirst = #4/1/2008#, dtmLast = #4/30/2008#
DateDiff(DateInterval.Weekday, dtmFirst, dtmLast, intDOW)

now I would think from reading.. that i would end up with 5, but i am ending
up with with 4?

Can someone help me with this?

Thanks

Brian
Jun 27 '08 #1
5 1385
Brian,

Did you know that if you wrote your dates in a more ISO way, your code would
be better, like your international audience like in this newsgroup could
easier help you.

Literals are a little bit scripting language.
A dateTime has a constructor which follows ISO
dimFirst as new DateTime(2008,4,1)
The date is written now makes your code more readable for everybody in the
world, even in culyures who use European languages (with the exception of
the USA) where dd-MM-yy is more common (and real ISO is in some cultures
used).

Peter Proost (from Belgium) has made some code to calculate around weeks, as
here in Europe we are often working more with weeks (and periods of 4 weeks)
than with month's

http://www.vb-tips.com/DateTimeCalc.aspx

Cor
"Brian S." <bs********@community.nospamschreef in bericht
news:ur**************@TK2MSFTNGP02.phx.gbl...
Hi,
I am trying to calculate the number of DayOfWeek in a month.. for
instance.. this month April I want to know how many tuesdays there are in
this month, this year
I try to use
where intDOW = FirstDayOfWeek.Tuesday
dtmFirst = #4/1/2008#, dtmLast = #4/30/2008#
DateDiff(DateInterval.Weekday, dtmFirst, dtmLast, intDOW)

now I would think from reading.. that i would end up with 5, but i am
ending up with with 4?

Can someone help me with this?

Thanks

Brian

Jun 27 '08 #2
On Apr 18, 10:49*pm, "Brian S." <bsgalla...@community.nospamwrote:
Hi,
I am trying to calculate the number of DayOfWeek in a month.. for instance...
this month April *I want to know how many tuesdays there are in this month,
this year
I try to use
where intDOW = FirstDayOfWeek.Tuesday
dtmFirst = #4/1/2008#, dtmLast = #4/30/2008#
DateDiff(DateInterval.Weekday, dtmFirst, dtmLast, intDOW)

now I would think from reading.. that i would end up with 5, but i am ending
up with with 4?

Can someone help me with this?

Thanks

Brian
Option Strict On
Option Explicit On
Option Infer Off

Imports System
Imports System.IO
Imports System.Text.RegularExpressions

Module Module1

Sub Main()
Console.WriteLine(CountDayOfWeek(Date.Now, DayOfWeek.Tuesday))
End Sub

Function CountDayOfWeek(ByVal dt As Date, ByVal dow As DayOfWeek)
As Integer
dt = New Date(dt.Year, dt.Month, 1)
Dim lastDay As Date = dt.AddDays(Date.DaysInMonth(dt.Year,
dt.Month) - 1)
Dim count As Integer

While (dt <= lastDay)
If dt.DayOfWeek = dow Then
count += 1
End If
dt = dt.AddDays(1)
End While

Return count
End Function

End Module

HTH

--
Tom Shelton
Jun 27 '08 #3
I would recomend annyone to use ISO 8601 when creating date time variabels
from string values , as this is also supported by all known databases
ISO 8601 = Year , Month , Day in .Net with the t: switch even times are
suported
However what suprised me Cor is that you lecture the TS about about the ISO
way and provide a link to example code from Peter that talks about ISO 8601
where in the top of the code is written

Dim StartYearDate As Date = CDate("1-1-" & Year)
Dim EndYearDate As Date = CDate("31-12-" & Year)

wich should have been conform ISO 8601
Dim StartYearDate As Date = CDate(String.Concat(year, "-01-01"))

Dim EndYearDate As Date = CDate(String.Concat(year, "-12-31"))

the code will work now because cdate is so smart however if you write code
with a date like this

Dim EndYearDate As Date = CDate("06-07-" & year)

this would give you in europe July 6 but in the US June 7 while

Dim EndYearDate As Date = CDate( string.concat(year,"-07-06"))

this wil give you in both situations July 6

regards
Michel
"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:87**********************************@microsof t.com...
Brian,

Did you know that if you wrote your dates in a more ISO way, your code
would be better, like your international audience like in this newsgroup
could easier help you.

Literals are a little bit scripting language.
A dateTime has a constructor which follows ISO
dimFirst as new DateTime(2008,4,1)
The date is written now makes your code more readable for everybody in the
world, even in culyures who use European languages (with the exception of
the USA) where dd-MM-yy is more common (and real ISO is in some cultures
used).

Peter Proost (from Belgium) has made some code to calculate around weeks,
as here in Europe we are often working more with weeks (and periods of 4
weeks) than with month's

http://www.vb-tips.com/DateTimeCalc.aspx

Cor
"Brian S." <bs********@community.nospamschreef in bericht
news:ur**************@TK2MSFTNGP02.phx.gbl...
>Hi,
I am trying to calculate the number of DayOfWeek in a month.. for
instance.. this month April I want to know how many tuesdays there are
in this month, this year
I try to use
where intDOW = FirstDayOfWeek.Tuesday
dtmFirst = #4/1/2008#, dtmLast = #4/30/2008#
DateDiff(DateInterval.Weekday, dtmFirst, dtmLast, intDOW)

now I would think from reading.. that i would end up with 5, but i am
ending up with with 4?

Can someone help me with this?

Thanks

Brian


Jun 27 '08 #4
Michel,

After sending the message I was aware of that.

Still I think that writting a date time in ISO is the best way to overcome
confusions.

Cor
"Michel Posseth [MCP]" <ms****@posseth.comschreef in bericht
news:er**************@TK2MSFTNGP06.phx.gbl...
>I would recomend annyone to use ISO 8601 when creating date time variabels
from string values , as this is also supported by all known databases
ISO 8601 = Year , Month , Day in .Net with the t: switch even times are
suported
However what suprised me Cor is that you lecture the TS about about the
ISO way and provide a link to example code from Peter that talks about ISO
8601
where in the top of the code is written

Dim StartYearDate As Date = CDate("1-1-" & Year)
Dim EndYearDate As Date = CDate("31-12-" & Year)

wich should have been conform ISO 8601
Dim StartYearDate As Date = CDate(String.Concat(year, "-01-01"))

Dim EndYearDate As Date = CDate(String.Concat(year, "-12-31"))

the code will work now because cdate is so smart however if you write code
with a date like this

Dim EndYearDate As Date = CDate("06-07-" & year)

this would give you in europe July 6 but in the US June 7 while

Dim EndYearDate As Date = CDate( string.concat(year,"-07-06"))

this wil give you in both situations July 6

regards
Michel
"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:87**********************************@microsof t.com...
>Brian,

Did you know that if you wrote your dates in a more ISO way, your code
would be better, like your international audience like in this newsgroup
could easier help you.

Literals are a little bit scripting language.
A dateTime has a constructor which follows ISO
dimFirst as new DateTime(2008,4,1)
The date is written now makes your code more readable for everybody in
the world, even in culyures who use European languages (with the
exception of the USA) where dd-MM-yy is more common (and real ISO is in
some cultures used).

Peter Proost (from Belgium) has made some code to calculate around weeks,
as here in Europe we are often working more with weeks (and periods of 4
weeks) than with month's

http://www.vb-tips.com/DateTimeCalc.aspx

Cor
"Brian S." <bs********@community.nospamschreef in bericht
news:ur**************@TK2MSFTNGP02.phx.gbl...
>>Hi,
I am trying to calculate the number of DayOfWeek in a month.. for
instance.. this month April I want to know how many tuesdays there are
in this month, this year
I try to use
where intDOW = FirstDayOfWeek.Tuesday
dtmFirst = #4/1/2008#, dtmLast = #4/30/2008#
DateDiff(DateInterval.Weekday, dtmFirst, dtmLast, intDOW)

now I would think from reading.. that i would end up with 5, but i am
ending up with with 4?

Can someone help me with this?

Thanks

Brian


Jun 27 '08 #5
Michel,

I forgot to write, I was hoping nobody saw it.

:-)

Cor

"Michel Posseth [MCP]" <ms****@posseth.comschreef in bericht
news:er**************@TK2MSFTNGP06.phx.gbl...
>I would recomend annyone to use ISO 8601 when creating date time variabels
from string values , as this is also supported by all known databases
ISO 8601 = Year , Month , Day in .Net with the t: switch even times are
suported
However what suprised me Cor is that you lecture the TS about about the
ISO way and provide a link to example code from Peter that talks about ISO
8601
where in the top of the code is written

Dim StartYearDate As Date = CDate("1-1-" & Year)
Dim EndYearDate As Date = CDate("31-12-" & Year)

wich should have been conform ISO 8601
Dim StartYearDate As Date = CDate(String.Concat(year, "-01-01"))

Dim EndYearDate As Date = CDate(String.Concat(year, "-12-31"))

the code will work now because cdate is so smart however if you write code
with a date like this

Dim EndYearDate As Date = CDate("06-07-" & year)

this would give you in europe July 6 but in the US June 7 while

Dim EndYearDate As Date = CDate( string.concat(year,"-07-06"))

this wil give you in both situations July 6

regards
Michel
"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:87**********************************@microsof t.com...
>Brian,

Did you know that if you wrote your dates in a more ISO way, your code
would be better, like your international audience like in this newsgroup
could easier help you.

Literals are a little bit scripting language.
A dateTime has a constructor which follows ISO
dimFirst as new DateTime(2008,4,1)
The date is written now makes your code more readable for everybody in
the world, even in culyures who use European languages (with the
exception of the USA) where dd-MM-yy is more common (and real ISO is in
some cultures used).

Peter Proost (from Belgium) has made some code to calculate around weeks,
as here in Europe we are often working more with weeks (and periods of 4
weeks) than with month's

http://www.vb-tips.com/DateTimeCalc.aspx

Cor
"Brian S." <bs********@community.nospamschreef in bericht
news:ur**************@TK2MSFTNGP02.phx.gbl...
>>Hi,
I am trying to calculate the number of DayOfWeek in a month.. for
instance.. this month April I want to know how many tuesdays there are
in this month, this year
I try to use
where intDOW = FirstDayOfWeek.Tuesday
dtmFirst = #4/1/2008#, dtmLast = #4/30/2008#
DateDiff(DateInterval.Weekday, dtmFirst, dtmLast, intDOW)

now I would think from reading.. that i would end up with 5, but i am
ending up with with 4?

Can someone help me with this?

Thanks

Brian


Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by David Mitchell | last post: by
18 posts views Thread by ChadDiesel | last post: by
8 posts views Thread by simon | last post: by
14 posts views Thread by Tommy Jakobsen | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.