Results 1 to 5 of 5

Thread: Opposite weekOfyear

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    3

    Opposite weekOfyear

    Hi,

    with this code I get the week number of the year:

    Code:
    Imports System.Globalization
    
    Public Class Form1
        Dim dateNow = DateTime.Now
        Dim dfi = DateTimeFormatInfo.CurrentInfo
        Dim calendar = dfi.Calendar
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' using Thursday because I can.
            Dim weekOfyear = calendar.GetWeekOfYear(dateNow, dfi.CalendarWeekRule, DayOfWeek.Thursday)
    
            Label1.Text = weekOfyear
        End Sub
    End Class
    Should I do the opposite, that is, from the weekOfyear derive the date (dd/mm/yyyy)…? thanks..

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    Re: Opposite weekOfyear

    Quote Originally Posted by tomi24 View Post
    Should I do the opposite
    Do you actually mean "how can I do the opposite"? Assuming you do, how do you think you should do it? The logic should pretty simple so how about you put some thought into first and then ask us to help if it does work. You don't actually need any programming experience to come up with the logic, so anyone can do that. You just need to work out what the dates for the first week are, based on the day of the week the year starts on, and then add the appropriate multiple of 7 days. Make some effort on your own behalf first, then we can help you fix it if it doesn't work. You don't even know that you can't do it if you don't even try. Trying and failing is part of learning so, if you want to learn, don't avoid it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    3

    Re: Opposite weekOfyear

    Apart from the various formulas and logics etc… and this was quite simple…

    I meant if there was a function already made like calendar.GetWeekOfYear that by giving the number of weeks and the year returned the date…

    I was looking for something like this:

    Code:
    Label1.Text = DateAdd("ww", NSett - 1, DateSerial(Year(Now), 1, 1))
    anyway thanks for the interest…

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,812

    Re: Opposite weekOfyear

    Quote Originally Posted by tomi24 View Post
    Apart from the various formulas and logics etc… and this was quite simple…

    I meant if there was a function already made like calendar.GetWeekOfYear that by giving the number of weeks and the year returned the date…

    I was looking for something like this:

    Code:
    Label1.Text = DateAdd("ww", NSett - 1, DateSerial(Year(Now), 1, 1))
    anyway thanks for the interest…
    I feel like I'm missing it, but is this what you want,

    Code:
        Public Function DateFromWeekNum(year As Integer, WeekNumber As Integer) As Date
            Dim rv As New Date(year, 1, 1)
            rv = rv.AddDays(WeekNumber * 7)
            Return rv
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,669

    Re: Opposite weekOfyear

    What date should it return tho? At best it can only return the date of the beginning of the week... you can give GetWeekOfYear a date and it returns which week that date is on ... but given a week, you can't restore the original date. Also typical functions in libraries tend to be used a lot. My guess is that since people already have the date going from date to week of year is much more commonly done, than going the other way around.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width