Page 1 of 1
[Solved] Print number of days in every month
Posted: Wed Jan 15, 2025 3:30 am
by luofeiyu
I want to print days in the every month from 1 to 12:
Code: Select all
sub daysEveryMonth
for i =1 to 12
days = number_of_day_in_the_month_i
print days
end sub
How to write some code to get the number of days in the month?
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 5:18 am
by FJCC
Well, it could be as simple as the following macro. Without knowing if you have a larger goal, it's hard to adjust the answer to your needs.
Code: Select all
Sub DaysForEveryMonth
MonthNames = array("Jan","Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
MonthDays = array(31,28,31,30,31,30,31,31,30,31,30,31)
for i = 0 to 11
print MonthNames(i), MonthDays(i)
next i
End Sub
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 5:35 am
by luofeiyu
You have specified it with:
MonthNames = array("Jan","Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
MonthDays = array(31,28,31,30,31,30,31,31,30,31,30,31)
It is not a smart way,a bit of stupid.
How can print all days in every month in 2024,2025,2026?
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 9:12 am
by Hagar Delest
luofeiyu wrote: ↑Wed Jan 15, 2025 5:35 am
It is not a smart way,a bit of stupid.
Since you have not provided any background, as said by FJCC, then it's difficult to provide the most appropriate answer.
Of course the proposed method is hardcoded but it is a quick one that could have suited your need if it were basic.
Not sure being so blunt when people try to help you is the best way to get further help.
First: have you done any homework? What have you tried already?
You should have a look at the
Survival Guide for the forum.
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 11:13 am
by karolus
How can print all days in every month in 2024,2025,2026?
Code: Select all
from datetime import date, timedelta as delta
stamp = date(2024,1,1)
while stamp <= date(2026,12,31):
print(f"{stamp.isoformat()}")
stamp+=delta(days=1)
not that stupid, but probably to smart for you?!
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 1:54 pm
by JeJe
Function for days in the month for a date
Code: Select all
Public Function DaysInMonth(ByVal d As Date) As Integer 'get days in month
DaysInMonth = Day(DateAdd("m", 1, d - Day(d) + 1) - 1)
End Function
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 2:10 pm
by Lupp
I also can't see a reasonable use-case for what the OQer asked for.
Anyway I give a commented solution on the supposedly understood level.
Code: Select all
Option Explicit
Function daysEveryMonth(pYear As Long)
REM The only month not having a fix number of days is
REM the second month of the year (february).
Dim result(1 To 12) As String, fa As Object, dimArray(), _
firstOfYear As String, isLeapYear As Boolean
dimArray = Array("", 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
fa = CreateUNOService("com.sun.star.sheet.FunctionAccess")
REM This way Calc functions can be called from any document.
firstOfYear = CDate(pYear & "-01-01")
isLeapYear = fa.callFunction("ISLEAPYEAR", Array(firstOfYear))
dimArray(2) = IIf(isLeapYear, 29, 28)
Redim Preserve dimArray(1 To 12)
daysEveryMonth = dimArray
End Function
Sub printDaysInMonths() REM Not useful, imo.
Dim yearNum As Long, result, i As Long
yearNum = InputBox("Enter the year (4 digits!):", "DaysInMonths", "" & Year(Now))
result = daysEveryMonth(yearNum)
For i = 1 To 12
Print Format(yearNum, "0000") & "-" & Format(i, "00") & " has " & result(i) & " days."
Next i
End Sub
A faint suspicion (No offence intended!):
Was this a homework in a beginner's class on programming in Basic?
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 2:13 pm
by Hagar Delest
Lupp wrote: ↑Wed Jan 15, 2025 2:10 pm
Was this a homework in a beginner's class on programming in Basic?
That's why I've never been comfortable with giving a solution right away before checking if at least some homework have been done.
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 2:22 pm
by JeJe
My solution might have come from here originally:
https://www.vbforums.com/showthread.php ... s-in-month
Instead of trying to reinvent the wheel, or asking here, do a search for "visual basic" or "VB6" and whatever
visual basic Function for days in the month
OO Basic is a close clone of VB and there's a big code base out there for VB and several solutions ready made if you do a search.
Re: Print number of days in every month
Posted: Wed Jan 15, 2025 3:02 pm
by JeJe
This is my code, using that function, which I use to create a monthly calendar page in Writer for printing
Code: Select all
sub printMonthCal()
oDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
dim d as date
'd=dateserial(CINT(2025),CINT(1),CINT(1))
d = date
addmonthcalendar(odoc,d)
end sub
sub addmonthcalendar(odoc,d as date)
oText = oDoc.getText()
vc = odoc.currentcontroller.viewcursor
vc.gotorange(otext.end,FALSE)
vc.CharHeight =15
if LEN(otext.string) >2 then
VC.COLLAPSETOEND
insertpagebreak
END IF
mm= array("","January","February","March","April","May","June","July","August","September","October","November","December")
vc.paraadjust =1
vc.string =mm(month(d))
vc.collapsetoend
oText.insertControlCharacter(VC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
vc.paraadjust =0
oCursor = oText.createTextCursorByRange(oText.getEND)
n = weekday(d)
sDays="*Sunday*Monday*Tuesday*Wednesday*Thursday*Friday*Saturday"
dim dnames() as string
dnames = split(sDays,"*")
Dim lHor as New com.sun.star.table.BorderLine
lHor.OuterLineWidth = 20
lHor.LineDistance = 0
with ocursor
nd = daysinmonth(d) -1
for i = 0 to nd
.TopBorder = lHor
.BottomBorderDistance =0
.TopBorderDistance = 0
.LeftBorderDistance = 0
.RightBorderDistance = 0
.ParaBottomMargin =180
.ParaIsConnectBorder =false
.CharHeight =15
.String = i+1 & " " & dnames(n)
.collapseToEnd
if i<> nd then .text.insertControlCharacter(oCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
n = n+1
if n= 8 then n =1
next
end with
End Sub
'*************helper
Public Function DaysInMonth(ByVal d As Date) As Integer 'get days in month
DaysInMonth = Day(DateAdd("m", 1, d - Day(d) + 1) - 1)
End Function
'************************/helper
Re: Print number of days in every month
Posted: Thu Jan 16, 2025 11:59 am
by luofeiyu
It is simple to get day number in any month with VBA:
Code: Select all
oYear = 2025
For oMonth = 1 To 12
DaysInMonth = Day(DateSerial(oYear, oMonth + 1, 1) - 1)
debug.print DaysInMonth
next
Re: Print number of days in every month
Posted: Thu Jan 16, 2025 12:15 pm
by JeJe
Your code runs in OO but fails for month 12.
Re: Print number of days in every month
Posted: Thu Jan 16, 2025 12:19 pm
by JeJe
If you put
at the top of the module it will run in OO as well.
Re: Print number of days in every month
Posted: Thu Jan 16, 2025 12:20 pm
by robleyd
I'm surprised it would run as posted, given the discrepancy between the variable declared as oYera and the variable oYear in the FOR loop.
Perhaps OP has used the
Do what I mean, not what I wrote flag

Re: Print number of days in every month
Posted: Thu Jan 16, 2025 12:26 pm
by JeJe
robleyd wrote: ↑Thu Jan 16, 2025 12:20 pm
I'm surprised it would run as posted, given the discrepancy between the variable declared as oYera and the variable oYear in the FOR loop.
This is why many advocate Option Explicit at the top of the module.
Re: Print number of days in every month
Posted: Thu Jan 16, 2025 12:46 pm
by luofeiyu
The first method:
Call VBA code in oobasic
Code: Select all
Option VBASupport 1
sub callvbinoobasic()
dim oYear,oMonth,DaysInMonth as integer
oYear = 2025
For oMonth = 1 To 12
DaysInMonth = Day(DateSerial(oYear, oMonth + 1, 1) - 1)
print DaysInMonth
next
end sub
The second method:
To call openoffice's worksheet function "daysinmonth",a bit ugly,to be improved:
Code: Select all
sub calldayfunction()
dim oFunctionAccess as object
dim aParameters(0) as string
dim oMonth,num as integer
dim mydate as date
oFunctionAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )
for oMonth =1 to 12
if oMonth < 10 then
mydate = "2025-0" & cstr(oMonth) & "-01"
else
mydate = "2025-" & cstr(oMonth) & "-01"
end if
aParameters(0) = mydate
num = oFunctionAccess.CallFunction( "daysinmonth", aParameters)
print num
next
end sub
If i know how to conert date type to the string, the code can be more smart.
Code: Select all
sub calldayfunction()
dim oFunctionAccess as object
dim aParameters(0) as integer
dim oMonth,num as integer
dim mydate as date
oFunctionAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )
for oMonth =1 to 12
mydate = DateSerial (2025, oMonth, 1)
aParameters(0) = convert_the_mydate_into_string #fix here
num = oFunctionAccess.CallFunction( "daysinmonth", aParameters)
print num
next
end sub
Re: Print number of days in every month
Posted: Thu Jan 16, 2025 1:08 pm
by JeJe
Code: Select all
for oMonth =1 to 12
num = oFunctionAccess.CallFunction( "daysinmonth", array("2025" & "-" & oMonth & "-" & "1"))
print num
next
Re: Print number of days in every month
Posted: Thu Jan 16, 2025 1:39 pm
by luofeiyu
Haha
Code: Select all
sub calldayfunction()
dim oFunctionAccess as object
dim aParameters(0) as string
dim oMonth,num as integer
dim mydate as string
oFunctionAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )
for oMonth =1 to 12
mydate = "2025-" & cstr(oMonth) & "-1"
aParameters(0) = mydate
num = oFunctionAccess.CallFunction( "daysinmonth", aParameters)
print oMonth,num
next
end sub