# Monday/Sunday of date

**URL:** <https://forum.kx.com/t/monday-sunday-of-date/9558>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [March 2, 2015, 12:46pm UTC](https://forum.kx.com/t/monday-sunday-of-date/9558 "2015-03-02T12:46:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![borota](https://avatars.discourse-cdn.com/v4/letter/b/f17d59/32.png) [@borota](https://forum.kx.com/u/borota)\
**Post date:** [March 2, 2015, 12:46pm UTC](https://forum.kx.com/t/monday-sunday-of-date/9558/1 "2015-03-02T12:46:00Z")

</div>

In SQL to get previous Monday of a date I do following:  
DATEADD(day,(DATEDIFF(day,‘1900-01-01’,GETDATE())/7)\*7,‘1900-01-01’)

To get following Sunday I do:

DATEADD(day,(DATEDIFF(day,‘1900-01-01’,GETDATE())/7)\*7,‘1900-01-07’)

Note that this gives Monday/Sunday irrespective of system locale/settings (on some systems beginning of week would be Monday, on others Sunday).

I could just translate that to q to get the same result, but I am wondering if there is a more concise/nicer way of getting it in q.

---

<div class="post-metadata">

**Author:** ![manni\_patel](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@manni\_patel](https://forum.kx.com/u/manni_patel)\
**Post date:** [March 2, 2015, 1:20pm UTC](https://forum.kx.com/t/monday-sunday-of-date/9558/2 "2015-03-02T13:20:00Z")

</div>

since day is an int since 2000.01.01 (a saturday), you can use mod 7to get the offset since saturday.z.d - .z.d mod 7And from there + or - to any day of week you want.On Mon, Mar 2, 2015 at 12:46 PM, Greg Borota wrote:\> In SQL to get previous Monday of a date I do following:\> DATEADD(day,(DATEDIFF(day,‘1900-01-01’,GETDATE())/7)\*7,‘1900-01-01’)\>\> To get following Sunday I do:\> DATEADD(day,(DATEDIFF(day,‘1900-01-01’,GETDATE())/7)\*7,‘1900-01-07’)\>\> Note that this gives Monday/Sunday irrespective of system locale/settings\> (on some systems beginning of week would be Monday, on others Sunday).\>\> I could just translate that to q to get the same result, but I am wondering\> if there is a more concise/nicer way of getting it in q.\>\>\> –\>

Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![borota](https://avatars.discourse-cdn.com/v4/letter/b/f17d59/32.png) [@borota](https://forum.kx.com/u/borota)\
**Post date:** [March 2, 2015, 3:56pm UTC](https://forum.kx.com/t/monday-sunday-of-date/9558/3 "2015-03-02T15:56:00Z")

</div>

Wow, this is beautiful.
