neojakey
neojakey

Reputation: 1663

Find This Weeks Monday

How do I get the date of this current week's Monday. Where Monday is the first day of the week. So if I was to look up this weeks it would return the date 1/16/2012

I am using VBScript and ASP

Many thanks in advance..

Paul

Upvotes: 6

Views: 10199

Answers (4)

Christopher Kennedy
Christopher Kennedy

Reputation: 1

I know the topic is a bit old, but I figured I'd share my working solution that I think is a bit more concise. Where dDate is your current date:

dDate = DateAdd("d", -(WeekDay(dDate, vbMonday) - 1), dDate)

The second parameters of WeekDay is the day you want the week to 'start'. For me, in the US, WeekDay corresponds the default second parameter to vbSunday. Specifying vbMonday, we just get the difference (base 1) between our current weekday and Monday. Subtract 1 and add the inverse to your current date should obtain your result.

Example:

WeekDay(2018-03-20, vbMonday) = -((2) - 1) = DateAdd("d", -1, 2018-03-20)

Upvotes: 0

drf
drf

Reputation: 8709

Effectively, the Weekday function returns Sunday=1, Monday=2, etc. To get the Monday of the same week, you want to subtract:

Sunday (1): 6 days
Monday (2): 0 days
Tuesday(3): 1 day
...
Saturday(7): 5 days.

Or Days to subtract = (Weekday - 2 + 7) Mod 7

So if d is a date, the Monday of the same week can be written as:

mondayofsameweek = DateAdd("d", -((Weekday(d) + 7 - 2) Mod 7), d)

Upvotes: 12

jonkroll
jonkroll

Reputation: 15722

In VBScript WeekDay returns the day of the week, starting with Sunday=1 (VBScript can be quirky like that). So just subtract two (Monday=2) from that value and call DateAdd.

monday = DateAdd("d",(WeekDay(Date())-2),Date())

Upvotes: 1

Tony Hopkinson
Tony Hopkinson

Reputation: 20330

VBScript has a function called WeekDay, it return 1 - 7, not sure whether 1 is Monday though, usually you can twiddle with that.

Either way get the weekday Thursday = 4? , so then you just need to take three days off your date with thae DateAdd function

Upvotes: 1

Related Questions