473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1504
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: -...
4
by: Alicia | last post by:
I am having a problem grouping by week. I am looking for the simpliest way of doing it in Microsoft Access. I have tried to use a pre-loaded calender, access did not like it at all. If there is...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
4
by: M | last post by:
Hello, I have code like dt.DayOfWeek.ToString(CultureInfo.CurrentCulture) where dt is a DateTime object. I get something like "Sunday", "Monday", etc... while I'm expecting it to be in...
8
by: simon | last post by:
dateTime sellDate; int16 dayOfWeek; dayOfWeek=sellDate.DayOfWeek I would like to set that monday is the first day of a week and not sunday. Any idea? In VB there is a function, where you...
1
by: palithawarnasiri | last post by:
In an access form I need to count number of records in the running month. Otherwise I need increamental number in a field mentioning serial number. First day of the month first sample should be 1...
2
by: crabb | last post by:
I would like to know if anyone knows how to set up an app that would count the number of specific days in a user defined year. example, how many Sundays, Mondays, Tuesdays, etc. in 2007 or a user...
1
LoanB
by: LoanB | last post by:
Hey gang Need some help please. Below I have code which returns the week number of the year. I need some code which simalarly return the week number of the current month. dtDate below gets...
14
by: Tommy Jakobsen | last post by:
Hi. Is there a method in .NET that takes "year" as an argument and returns the total number of weeks in that year? For culture da-DK (Danish). Thanks in advance. Tommy.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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
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
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,...

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.