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
Comments
See %SYSTEM.SQL (LASTDAY, DAYOFMONTH, etc.)
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?KE…
https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KE…
Thanks
PERFECT THANK YOU
grateful.
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
PERFECT THANK YOU
grateful.
<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)
I like to be in chage of my own code. If I want the $h of the last day of the month
I code the first day of the following month resolve into hDate=($zdh("01 MMM yyyy",2)-1
Now you can use it with $zd(hDate,2,,4)
PERFECT THANK YOU
grateful.
An example without complex formulas and very fast
Include %DeepSee
Class dc.test [ Abstract ]
{
ClassMethod FirstLastDayMonth(
year = 2015,
month = 2)
{
s mm=$$$iscPadZero(month,2),
firstDay=$zdh(year_mm_"01",8),
lastDay=$zdh(year_mm_$$$iscDaysInMonth(year,month),8),
firstDayName=$zd(firstDay,12,,,,,,,,1),
lastDayName=$zd(lastDay,12,,,,,,,,1)
w $$$FormatText("firstDay = %1 (%2), lastDay = %3 (%4)",$zd(firstDay),firstDayName,$zd(lastDay),lastDayName),!
}
/// d ##class(dc.test).Test()
ClassMethod Test()
{
d ..FirstLastDayMonth(2015,2),
..FirstLastDayMonth(2021,9)
}
}USER>d ##class(dc.test).Test()
firstDay = 01.02.2015 (Sunday), lastDay = 28.02.2015 (Saturday)
firstDay = 01.09.2021 (Wednesday), lastDay = 30.09.2021 (Thursday)