How do I get a date from the week number and year?

Posted on 2008-11-14
Last Modified: 2012-05-05
I need to get the date of the first day in a given week (Sunday is the 1st day of the week). I'm using VB.Net in Visual Studio 2005. For example, if I pass it week# 46 and the year 2008 I should get the date of Sunday, Nov. 9, 2008.
Question by:rkeith27
    LVL 23

    Accepted Solution

    This is problematic.  How do you want to handle week 1 if the year doesn't begin on a Sunday?
    I'll assume that the first week of the year begins on the Sunday of that week.

    Function DateOfThisWeek(ByVal weekNumber As Integer, ByVal year As Integer)
        Dim firstSunday As DateTime = DateTime.Parse(year & "/01/01").AddDays(-(DateTime.Parse(year & "/01/01").DayOfWeek))
        Return firstSunday.AddDays((weekNumber - 1) * 7.0)
    End Function

    Author Closing Comment


    Author Comment

    I'd handle the first week as the first 4 day week as week 1 with the start day of the week as Sunday.
    LVL 23

    Expert Comment

    by:Christopher Kile
    Yup, that's what I thought would work best.  I remember wrangling about it, though, more than once in different jobs.  Everyone has an opinion... :D

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now