views:

400

answers:

7

I need to find a way to find the date (DD/MM/YYYY) of the Monday for any week we're on.

For example, for this week, monday would be 09/11/2009, and if this were next week it'd be 16/11/2009.

I managed to get somewhere in the forms of code, but all I got was 'cannot convert to Integer' errors. I was using Date.Today and AddDays().

Thanks for any help. :)

A: 

DateTime.DayOfWeek is an enum that indicates what day a given date is. As Monday is 1, you can find the Monday of the current week using the following code:

Dim monday As DateTime = Today.AddDays((Today.DayOfWeek - DayOfWeek.Monday) * -1)
Jason Berkan
A: 
=Format(DateAdd("d", (-1 * WeekDay(Date.Today()) + 2), Date.Today()), "dd/MM/yyyy")
Registered User
+5  A: 
Dim today As Date = Date.Today
Dim dayDiff As Integer = today.DayOfWeek - DayOfWeek.Monday
Dim monday As Date = today.AddDays(-dayDiff)
Meta-Knight
Works perfectly, cheers. :)
Willis
You can set my answer as accepted then :-) Also if you want to comment something to us (like your thanks message) you should add a comment to your question or edit your question instead of adding an answer. Also, welcome on Stack Overflow!
Meta-Knight
A: 

A simple method should get you what you want:

    private static DateTime GetMondayForWeek(DateTime inputDate)
    {
        int daysFromMonday = inputDate.DayOfWeek - DayOfWeek.Monday;
        return inputDate.AddDays(-daysFromMonday);
    }

You could also extend it for any day that you want as well:

    private static DateTime GetDayForWeek(DateTime inputDate, DayOfWeek inputDay)
    {
        int daysAway = inputDate.DayOfWeek - inputDay;
        return inputDate.AddDays(-daysAway);
    }

To call the first example just use something like:

DateTime mondayDate = GetMondayForWeek(new DateTime(2009, 11, 15));
Console.WriteLine(mondayDate);
Blake Blackwell
feeding C# to the new guy? (see tags)
Saul Dolgin
Ahhhh, didn't see the VB tag. I normally ask questions, not answer them so a little new to the whole answering scene. Unfortunately I don't even have VB on my machine.
Blake Blackwell
A: 

There is a day of week method that you can use

Dim instance As DateTime
Dim value As DayOfWeek

value = instance.DayOfWeek

see: http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx

Shiraz Bhaiji
A: 

I just did this in a project I'm working on --I promise, it's correct. This is a method that returns the nth monday after the given date. If the given date is a monday, it returns the next monday.

Public Function GetSubsequentMonday(ByVal startDate As DateTime, ByVal subsequentWeeks As Integer) As DateTime
    Dim dayOfWeek As Integer = CInt(startDate.DayOfWeek)
    Dim daysUntilMonday As Integer = (Math.Sign(dayOfWeek) * (7 - dayOfWeek)) + 1
    'number of days until the next Monday
    Return startDate.AddDays(CDbl((daysUntilMonday + (7 * (subsequentWeeks - 1)))))
End Function
kmerkle
A: 

Thanks for everyone's help, problem sorted.

Willis
Then consider marking one of the answers as the correct one
Chris Dunaway