Is there a function or a way to get tomorrow’s date?
Ideally, I’d like to do something like this..
today + 3 and then check if that date is part of the current month or not.
Thanks,
David
You can find the last day of the current month and check if it’s beyond:
DayMax = 31
If Month = 4 or Month = 6 or Month = 9 Then
DayMax = 30
Endif
If Month = 2 then
DayMax = 28
If year mod 4 = 0 then
If year mod 100 = 0 then
If year mod 400 = 0 then
DayMax = 29
Endif
Else
DayMax = 29
Endif
Endif
Endif
This is beautiful! How do I grab currentdate+3day’s date to compare to the max?
But I am not sure if this helps.. as I want a count of business days remaining in the month.
Current Year is YEAR
Current Month is MONTH
Current Day is DAY.
If you need to extract the day from a date, say 20190326, then you’ll have to divide it by 1000000 (it’s a common number for Prt), then round it to the lowest integer, then multiply it by 1000000, then compute the difference between the date and the number you have got:
x = date
y = round((x / 1000000) - 0.5)
z = y * 1000000
d = date - z
d is the DAY number.
This is a link to my code to compute business days in a month https://www.prorealcode.com/topic/trading-in-giorni-specifici/#post-64933
“Giornoferiale” means business day.
IF OpenMonth <> OpenMonth[1] THEN //Se il mese della barra attuale è diverso da quello della barra precedente significa che siamo alla prima barra del mese…
BusinessDay = 0 //… per cui dobbiamo ricominciare da 0 a contare i giorni lavorativi
ENDIF
IF OpenDayOfWeek >= 1 AND OpenDayOfWeek <= 5 AND IntraDayBarIndex = 0 THEN //Se siamo tra lunedì (1) e venerdì (5) ed è la prima barra del giorno…
BusinessDay = BusinessDay + 1 //… significa che è iniziato un nuovo giorno lavorativo.
ENDIF
But how can I create a function that returns 3 when it checks today’s date 20190326?
Using the above would give me the current businessday count for current date. Not sure how to get businessday count for last day of the month. if I had that value can do simple subtraction.
Sorry, my previous code was a divion (then multiplication) by 100, not 1000000!
Just modify my previous DATE code:
x = date //x = 20190326
y = round((x / 100) - 0.5) //y = 201903.00
z = y * 100 //z = 20190300
d = x - z //d = 26
into Get MONTH:
x = date //x = 20190326
y = round((x / 100) - 0.5) //y = 201903.00
z = round((y / 100) - 0.5) //z = 2019.0000
m = y - z //m = 3
It is not making sense. How can I combine what you’re providing to return the number of business days left in a month? Can you show me a function that takes today’s date 20160326 and returns 3. The same function should take tomorrow’s date and return 2.
day of the week number can be retrieved with: (Replace myYear,myMonth and myDay with YYYY, MM, DD number format. In this example it basically returns the day of the week of the current day.), I think that we could associate the snippets from Roberto to this one to know the last business day.
//find the day number with a date
myYear=Year
myMonth=Month
myDay=Day
if myMonth >= 3 then
D = (((23*myMonth)/9) + myDay + 4 + myYear + (myYear/4) - (myYear/100) + (myYear/400) - 2) mod 7
else
z = myYear - 1
D = (((23*myMonth)/9) + myDay + 4 + myYear + (z/4) - (z/100) + (z/400) ) mod 7
endif
return d
There’s no built-in function, by combining the above codes (extracting from dates) and business day math you can achieve that, but it takes a loop, since being 3 does not mean there have been 3 busniss days. PRT does not support easy date math (like eXcel does).
You’ll have to start a loop from the day you want, then go backwords counting only those days whose dayofweek is between 1 and 5 to find out how many business day there were BEFORE that day.
It’s not as straightfoward!
I cannot understand how to replace your ‘OpenDayOfWeek’ in the businessday logic.
Is there a way to get the day of the week from the date?
My temporary idea in pseudo code
x = date
DayMax = getDayMax(x)
businessdayleft = 0
for count = 1 to DayMax
new date = x + count
if dayNumber(newdate) > DayMax then break
if getDayOfWeek(newdate) is business then increment bussinessdaysleft by 1
// basically I need a dayNumber and dayOfWeek function unless I’m missing something?
we can use Nicolas’s solution for dayNumber, but am still unsure how to extract dayOfWeek
OpenDayOfWeek returns:
1=Monday, … 5=Friday, so if any OpenDayOfWeek is within the range 1-5 it’s a business day (unless there’s some local holiday), so you can detect how many business days there are in each month by tallying from day 1 to the last day and skipping those ourside that range.
Anyway, I attach 4 functions (they are actually indicators, but the behaviour is the same as in other languages, they may require parameters as their input and return some data):
- DayMax (input: Month,Year returns: MaxDay)
- IsLeapYear (input: Year returns: 1=leap year, 0=no leap year)
- UnpackDate (input: Date formatted as YYYYMMDD, as PRT does returns: Day,Month,Year)
- GetDayOfWeek (input: Date formatted as YYYYMMDD, as PRT does returns: 0=Sunday,1=Monday,….,6=Saturday)
I attach both .ITF files to be imported into ProBuilder as well as .TXT files:
//input: MONTH,YEAR
//requires indicator ISLEAPYEAR
//
MaxDay = 31
If MyMonth = 4 or MyMonth = 6 or MyMonth = 9 or MyMonth = 11 Then
MaxDay = 30
Endif
If MyMonth = 2 then
x = CALL "IsLeapYear"[MyYear]
MaxDay = 28 + x
Endif
RETURN MaxDay
//input: YEAR
LeapYear = 0
If MyYear mod 4 = 0 then
If MyYear mod 100 = 0 then
If MyYear mod 400 = 0 then
LeapYear = 1
Endif
Else
LeapYear = 1
Endif
Endif
RETURN LeapYear
//input: DATE
d = 0
m = 0
y = 0
// example:
x = MyDate //x = 20190326
w = round((x / 100) - 0.5) //w = 201903.00
z = w * 100 //z = 20190300
d = x - z //d = 26
//
y = round((w / 100) - 0.5) //z = 2019.0000
m = w - (y * 100) //m = 3
//
RETURN d,m,y
//input: DATE
//requires indicators UNPACKDATE and ISLEAPYEAR
//
// https://www.wikihow.it/Calcolare-il-Giorno-della-Settimana (method 1)
//
MyDay, MyMonth, MyYear = CALL "UnpackDate"[MyDate]
IF MyMonth = 1 OR MyMonth = 10 THEN
Mvalue = 0
ELSIF MyMonth = 4 OR MyMonth = 7 THEN
Mvalue = 6
ELSIF MyMonth = 9 OR MyMonth = 12 THEN
Mvalue = 5
ELSIF MyMonth = 5 THEN
Mvalue = 1
ELSIF MyMonth = 6 THEN
Mvalue = 4
ELSIF MyMonth = 8 THEN
Mvalue = 2
ENDIF
d = MyDay + Mvalue
WHILE d > 6
d = d - 7
WEND
y = MyYear MOD 100
Century = MyYear - y
Century = (Century MOD 400) / 100
IF Century = 1 THEN
Century = 5
ELSIF Century = 2 THEN
Century = 3
ELSIF Century = 3 THEN
Century = 1
ENDIF
z = y
WHILE y > 27
y = y - 28
WEND
y = y + round((z / 4) - 0.5) + Century
IF MyMonth < 3 THEN
x = CALL "IsLeapYear"[MyYear]
y = y - x
ENDIF
d = d + y
WHILE d > 6
d = d - 7
WEND
IF d = 0 THEN
d = 7
ENDIF
RETURN d - 1 //0=Sunday, 1=Monday...... 6=Saturday
Edit: The above GetDayOfWeek function does not work fine, it has been replaced by a new version (see post https://www.prorealcode.com/topic/tomorrows-date/page/2/#post-95272)