Code: Select all
sub daysEveryMonth
for i =1 to 12
days = number_of_day_in_the_month_i
print days
end sub
Code: Select all
sub daysEveryMonth
for i =1 to 12
days = number_of_day_in_the_month_i
print days
end sub
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
Since you have not provided any background, as said by FJCC, then it's difficult to provide the most appropriate answer.
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)
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
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
That's why I've never been comfortable with giving a solution right away before checking if at least some homework have been done.
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.visual basic Function for days in the month
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
Code: Select all
oYear = 2025
For oMonth = 1 To 12
DaysInMonth = Day(DateSerial(oYear, oMonth + 1, 1) - 1)
debug.print DaysInMonth
next
Code: Select all
Option VBASupport 1
Roses are Red, Violets are Blue]
Unexpected '{' on line 32
.This is why many advocate Option Explicit at the top of the module.
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
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
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
Code: Select all
for oMonth =1 to 12
num = oFunctionAccess.CallFunction( "daysinmonth", array("2025" & "-" & oMonth & "-" & "1"))
print num
next
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