[Solved] Print number of days in every month

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

[Solved] Print number of days in every month

Post 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?
Last edited by luofeiyu on Thu Jan 16, 2025 1:39 pm, edited 1 time in total.
LibreOffice 7.4.7.2 on Debian 12
FJCC
Moderator
Posts: 9539
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Print number of days in every month

Post 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
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

Re: Print number of days in every month

Post 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?
LibreOffice 7.4.7.2 on Debian 12
User avatar
Hagar Delest
Moderator
Posts: 33346
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Print number of days in every month

Post 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.
LibreOffice 25.2 on Linux Mint Debian Edition (LMDE Faye) and 24.8 portable on Windows 11.
User avatar
karolus
Volunteer
Posts: 1226
Joined: Sat Jul 02, 2011 9:47 am

Re: Print number of days in every month

Post 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?!
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 24.8… flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Print number of days in every month

Post 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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3693
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Print number of days in every month

Post 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?
Last edited by Lupp on Wed Jan 15, 2025 2:25 pm, edited 1 time in total.
On Windows 10: LibreOffice 25.2.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Hagar Delest
Moderator
Posts: 33346
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Print number of days in every month

Post 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.
LibreOffice 25.2 on Linux Mint Debian Edition (LMDE Faye) and 24.8 portable on Windows 11.
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Print number of days in every month

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Print number of days in every month

Post 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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

Re: Print number of days in every month

Post 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 
Last edited by luofeiyu on Thu Jan 16, 2025 12:26 pm, edited 1 time in total.
LibreOffice 7.4.7.2 on Debian 12
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Print number of days in every month

Post by JeJe »

Your code runs in OO but fails for month 12.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Print number of days in every month

Post by JeJe »

If you put

Code: Select all

Option VBASupport 1
at the top of the module it will run in OO as well.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
robleyd
Moderator
Posts: 5383
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Print number of days in every month

Post 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 :evil:
Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 25.2.4.3; SlackBuild for 25.2.4 by Eric Hameleers
---------------------
Roses are Red, Violets are Blue]
Unexpected '{' on line 32
.
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Print number of days in every month

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

Re: Print number of days in every month

Post 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
LibreOffice 7.4.7.2 on Debian 12
JeJe
Volunteer
Posts: 3064
Joined: Wed Mar 09, 2016 2:40 pm

Re: Print number of days in every month

Post by JeJe »

Code: Select all

    for oMonth =1 to 12
        num = oFunctionAccess.CallFunction( "daysinmonth", array("2025" & "-" & oMonth & "-" & "1"))
    print num
    next 
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

Re: Print number of days in every month

Post 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
LibreOffice 7.4.7.2 on Debian 12
Locked