Counting the Number of dayofweek in a month

B

Brian S.

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
 
C

Cor Ligthert[MVP]

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
 
T

Tom Shelton

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
 
M

Michel Posseth [MCP]

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
 
C

Cor Ligthert[MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top