Hi,
I have written a function which allows me to return the number of Sundays between any two given dates, i.e. f[firsdate; last date] which is run for the year 2014: f[2014.01.01; 2014.12.31] returns a value of 52 (There were 52 Sundays in 2014). I am wondering if there is a way that I can only count Sundays which fell on the first of any given month of any year? For example, in 2014 this happened once.
Thanks.
q){(“d”$(“m”$x)+til 12)inter a where 1=mod[;7]a:x+til 1+y-x}[2014.01.01;2014.12.31]
,2014.06.01
You can put a count in there if you just want the count.
Terry
Hi,
Firstly I’d suggest counting the number of first of the months which are Sundays, rather than vice versa, as it will require fewer checks. You could use the following function:
{[sd;ed] sm:month$sd;em:
month$ed;sum 1=mod[;7]`date$sm+til 1+em-sm}[2014.01.01;2014.12.31]
This function casts the start and end date to months, generates a list of intervening months, then casts back to dates (casting a month to a date automatically chooses the first day of the month). It then uses mod to check which day the resulting dates are (0 - Saturday, etc) and checks which are Sundays, summing the result for the count.
Regards,
Sam
From: personal-kdbplus@googlegroups.com <personal-kdbplus@googlegroups.com> on behalf of Diarmaid Doherty <diarmaid-doherty@hotmail.com>
Sent: 12 June 2019 15:10:20
To: Kdb+ Personal Developers
Subject: [personal kdb+] Extracting Dates
Hi,
I have written a function which allows me to return the number of Sundays between any two given dates, i.e. f[firsdate; last date] which is run for the year 2014: f[2014.01.01; 2014.12.31] returns a value of 52 (There were 52 Sundays in 2014). I am wondering if there is a way that I can only count Sundays which fell on the first of any given month of any year? For example, in 2014 this happened once.
Thanks.
–
Submitted via Google Groups
It’d be much easier to perform the checks in reversed order, i.e., check among the first days of each month for Sundays. The code below demonstrates this:
f:{[begin;end] sum 1=mod[;7]date${x+1+til y-x}.
month$(begin;end) }