Counting the Number of dayofweek in a month

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian S.

    #1

    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(DateIn terval.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


  • Cor Ligthert[MVP]

    #2
    Re: Counting the Number of dayofweek in a month

    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



    Cor


    "Brian S." <bsgallatin@com munity.nospamsc hreef in bericht
    news:urSC7idoIH A.1236@TK2MSFTN GP02.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(DateIn terval.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
    >
    >

    Comment

    • Tom Shelton

      #3
      Re: Counting the Number of dayofweek in a month

      On Apr 18, 10:49 pm, "Brian S." <bsgalla...@com munity.nospamwr ote:
      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(DateIn terval.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.Reg ularExpressions

      Module Module1

      Sub Main()
      Console.WriteLi ne(CountDayOfWe ek(Date.Now, DayOfWeek.Tuesd ay))
      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

      Comment

      • Michel Posseth [MCP]

        #4
        Re: Counting the Number of dayofweek in a month

        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.Co ncat(year, "-01-01"))

        Dim EndYearDate As Date = CDate(String.Co ncat(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(y ear,"-07-06"))

        this wil give you in both situations July 6



        regards


        Michel


        "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
        news:87B630C1-AC07-4FD9-A31F-7909C1DFFAB2@mi crosoft.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
        >

        >
        Cor
        >
        >
        "Brian S." <bsgallatin@com munity.nospamsc hreef in bericht
        news:urSC7idoIH A.1236@TK2MSFTN GP02.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(DateI nterval.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
        >>
        >>
        >

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Counting the Number of dayofweek in a month

          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]" <msnews@posseth .comschreef in bericht
          news:erN9eEwoIH A.2160@TK2MSFTN GP06.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.Co ncat(year, "-01-01"))
          >
          Dim EndYearDate As Date = CDate(String.Co ncat(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(y ear,"-07-06"))
          >
          this wil give you in both situations July 6
          >
          >
          >
          regards
          >
          >
          Michel
          >
          >
          "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
          news:87B630C1-AC07-4FD9-A31F-7909C1DFFAB2@mi crosoft.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." <bsgallatin@com munity.nospamsc hreef in bericht
          >news:urSC7idoI HA.1236@TK2MSFT NGP02.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(Date Interval.Weekda y, 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
          >>>
          >>>
          >>
          >
          >

          Comment

          • Cor Ligthert[MVP]

            #6
            Re: Counting the Number of dayofweek in a month

            Michel,

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

            :-)

            Cor

            "Michel Posseth [MCP]" <msnews@posseth .comschreef in bericht
            news:erN9eEwoIH A.2160@TK2MSFTN GP06.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.Co ncat(year, "-01-01"))
            >
            Dim EndYearDate As Date = CDate(String.Co ncat(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(y ear,"-07-06"))
            >
            this wil give you in both situations July 6
            >
            >
            >
            regards
            >
            >
            Michel
            >
            >
            "Cor Ligthert[MVP]" <notmyfirstname @planet.nlschre ef in bericht
            news:87B630C1-AC07-4FD9-A31F-7909C1DFFAB2@mi crosoft.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." <bsgallatin@com munity.nospamsc hreef in bericht
            >news:urSC7idoI HA.1236@TK2MSFT NGP02.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(Date Interval.Weekda y, 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
            >>>
            >>>
            >>
            >
            >

            Comment

            Working...