Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Date by Week nr?

Date by Week nr?

Scheduled Pinned Locked Moved Visual Basic
question
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Snowjim
    wrote on last edited by
    #1

    Hey I have tryed to get date(Monday) by a week nr and year but this do not always work. Here is my code Public Function getDateByWeekNr(ByVal inWeekNr As Integer, ByVal inYear As Integer) As DateTime Dim cal As New System.Globalization.GregorianCalendar Dim dTime As DateTime dTime = New DateTime(inYear, 1, 1) For i As Integer = 0 To 365 If dTime.DayOfWeek = DayOfWeek.Monday Then Exit For Else dTime = dTime.Subtract(New TimeSpan(24, 0, 0)) End If Next Return cal.AddWeeks(dTime, inWeekNr) End Function what am i doing wrong?

    S 1 Reply Last reply
    0
    • S Snowjim

      Hey I have tryed to get date(Monday) by a week nr and year but this do not always work. Here is my code Public Function getDateByWeekNr(ByVal inWeekNr As Integer, ByVal inYear As Integer) As DateTime Dim cal As New System.Globalization.GregorianCalendar Dim dTime As DateTime dTime = New DateTime(inYear, 1, 1) For i As Integer = 0 To 365 If dTime.DayOfWeek = DayOfWeek.Monday Then Exit For Else dTime = dTime.Subtract(New TimeSpan(24, 0, 0)) End If Next Return cal.AddWeeks(dTime, inWeekNr) End Function what am i doing wrong?

      S Offline
      S Offline
      sathish s
      wrote on last edited by
      #2

      You could use something like this [CODE] Dim dTime As DateTime dTime = New DateTime(2006, 1, 1) Dim wkCtr As Integer Dim inWk As Integer While 1 If dTime.DayOfWeek = DayOfWeek.Monday Then wkCtr += 1 End If If dTime.DayOfWeek = DayOfWeek.Monday And wkCtr = inWk Then Exit While End If dTime = dTime.AddDays(1) End While [/CODE]

      S 3 Replies Last reply
      0
      • S sathish s

        You could use something like this [CODE] Dim dTime As DateTime dTime = New DateTime(2006, 1, 1) Dim wkCtr As Integer Dim inWk As Integer While 1 If dTime.DayOfWeek = DayOfWeek.Monday Then wkCtr += 1 End If If dTime.DayOfWeek = DayOfWeek.Monday And wkCtr = inWk Then Exit While End If dTime = dTime.AddDays(1) End While [/CODE]

        S Offline
        S Offline
        Snowjim
        wrote on last edited by
        #3

        Thanks, i will tray that, is this working even if the1 week of a year started last year?

        1 Reply Last reply
        0
        • S sathish s

          You could use something like this [CODE] Dim dTime As DateTime dTime = New DateTime(2006, 1, 1) Dim wkCtr As Integer Dim inWk As Integer While 1 If dTime.DayOfWeek = DayOfWeek.Monday Then wkCtr += 1 End If If dTime.DayOfWeek = DayOfWeek.Monday And wkCtr = inWk Then Exit While End If dTime = dTime.AddDays(1) End While [/CODE]

          S Offline
          S Offline
          Snowjim
          wrote on last edited by
          #4

          Thanks, i will try that, is this working even if the1 week of a year started last year?

          1 Reply Last reply
          0
          • S sathish s

            You could use something like this [CODE] Dim dTime As DateTime dTime = New DateTime(2006, 1, 1) Dim wkCtr As Integer Dim inWk As Integer While 1 If dTime.DayOfWeek = DayOfWeek.Monday Then wkCtr += 1 End If If dTime.DayOfWeek = DayOfWeek.Monday And wkCtr = inWk Then Exit While End If dTime = dTime.AddDays(1) End While [/CODE]

            S Offline
            S Offline
            Snowjim
            wrote on last edited by
            #5

            Thanks, i will try that, is this working even if the week of a year started last year?

            S 1 Reply Last reply
            0
            • S Snowjim

              Thanks, i will try that, is this working even if the week of a year started last year?

              S Offline
              S Offline
              sathish s
              wrote on last edited by
              #6

              Glad to help you. This will give you the Nth Monday of an year.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups