Question
· Sep 15, 2021

day of the month

Hello everyone
I use cache script, I would like to know from you if there is any function or class in the cache where I can get the start and end date of a given month:
Example: What is the first and last day of the month of February 2015.

Grateful.
Davidson

Product version: Caché 2018.1
$ZV: Cache for Windows (x86-64) 2018.1.5 (Build 659) Mon Mar 22 2021 07:15:21 EDT
Discussion (9)2
Log in or sign up to continue

Hi @Davidson Espindola 
First date of every month will be 1 which you can concat with current month and year however for last month you can use LAST_DAY cache function by passing any date of desire month.
SELECT LAST_DAY('2004-02-25')  

For details check below documentations:

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY...

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY...

Thanks

If you don't want to mess with SQL functions, this is still easy, especially taking advantage of date format 8 (ANSI):

DOM    ; SRS 2021-09-15 PUBLIC DOMAIN NO WARRENTY
    ; Return $HOROLOG date for first day of the month.
FIRST(y,m)    QUIT $ZDATEH(y*100+m*100+1,8)
    ; Return $HOROLOG date for last day of the month.
LAST(y,m)    SET m=m+1 SET:m>12 m=m-12,y=y+1
    QUIT $ZDATEH(y*100+m*100+1,8)-1

Test for this year with:

USER>FOR i=1:1:12 WRITE !,i," ",$$FIRST^DOM(2021,i)," ",$$LAST^DOM(2021,i)

1 65745 65775
2 65776 65803
3 65804 65834
4 65835 65864
5 65865 65895
6 65896 65925
7 65926 65956
8 65957 65987
9 65988 66017
10 66018 66048
11 66049 66078
12 66079 66109

<Nitpicking ON>

For the LAST() function you can also use just an expression:

LAST(y,m)  QUIT $ZDATEH(m=12+y*100+(m#12)+1*100+1,8)-1

<Nitpicking OFF>

By the way, if you are just interested, how many days a month in a given year has, there is a simple formula:

LastDay(y,m)  quit $s(m-2:m\8+m#2+30, 1:y#4=0+28)

In the above formula, it's OK to use short leap year calculation (y#4=0) for contemporary dates (date from 1901 until 2099) else case you have to stick to the long format: (y#4=0)-(y#100=0)+(y#400=0)

 
An example without complex formulas and very fast