Search code examples
kdb

Check if a given date is the last day of the month


Let's say I have a given date d:

d:2019.02.20

How to write a function f such that f is True if d is the last day of the month, False otherwise?

Example:

f[d]             / Should return 0b
f[2019.02.28]    / Should return 1b
f[2019.01.31]    / Should return 1b

Solution

  • You can extract the month part of the date with `mm$dt, or "m"$dt. See: https://code.kx.com/q4m3/7_Transforming_Data/#723-casts-that-narrow

    Then just compare with your input date +1 (which will add one day):

    q)f:{(`mm$x) <> `mm$x+1}
    
    q)f[2019.02.28 2020.02.28 2019.03.04 2019.03.31]
    1001b