<html>
<head>
<base href="https://bugs.documentfoundation.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_UNCONFIRMED "
title="UNCONFIRMED - BASIC: SerialDate() only accept the most obvious month and day values."
href="https://bugs.documentfoundation.org/show_bug.cgi?id=139363">139363</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>BASIC: SerialDate() only accept the most obvious month and day values.
</td>
</tr>
<tr>
<th>Product</th>
<td>LibreOffice
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>UNCONFIRMED
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>medium
</td>
</tr>
<tr>
<th>Component</th>
<td>BASIC
</td>
</tr>
<tr>
<th>Assignee</th>
<td>libreoffice-bugs@lists.freedesktop.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>guraknugen@gmail.com
</td>
</tr></table>
<p>
<div>
<pre>The BASIC command SerialDate() doesn't accept months and days out of bounds.
The Calc cell-function DATE() does and so does Excel's DateSerial(), and it
would be very practical if SerialDate() also did.
Example 1:
Print SerialDate(2021,2,29)
Gives an error message.
In a spreadsheet cell: =DATE(2021;2;29)
Result: 2021-03-01 (if cell is formatted properly, as YYYY-MM-DD, of course)
Expected result: 2021-03-01
Example 2:
Find the last date in February 2021:
Print SerialDate(2021,3,0)
Error message.
In a spreadsheet cell: =DATE(2021;3;0)
Result: 2021-02-28
Expected result: 2021-02-28
Example 3:
Just about anything, such as:
Print SerialDate(2021,-73,-4500)
Error message.
In a spreadsheet cell: =DATE(2021;-73;-4500)
Result: 2002-07-06
Expected result: 2002-07-06
Example 4:
Loop through months:
Dim dtCurrentDate As Date, dtDate As Date
Dim iYear As Integer, iMonth As Integer, i As Integer
dtCurrentDate=Now()
iYear=Year(dtCurrentDate)
iMonth=Month(dtCurrentDate)
For i=-100 To 100
dtDate=SerialDate(iYear, iMonth+i, 1)
' Print dtDate somewhere or whatever.
Next i
The above only works as long as 1≤(iMonth+i)≤12, otherwise there is an error.
Expected result: It should loop through all months and figure out the dtDate
values without questioning anything.
Workaround:
A custom function for it. Here are mine, I wrote two different global ones:
' This one adjusts the input values before using the built in SerialDate.
Public Function DateSerialA(iYear As Integer, iMonth As Integer, iDay As
Integer) As Date
If iMonth>12 Or iMonth<1 Then
Dim iYearDiff As Integer
iYearDiff=int((iMonth-1)/12)
iYear=iYear+iYearDiff
iMonth=iMonth-12*iYearDiff
End If
DateSerialA=DateSerial(iYear, iMonth, 1)+iDay-1
End Function
' This one simply use Calc's DATE() function.
Public Function DateSerialC(iYear As Integer, iMonth As Integer, iDay As
Integer) As Date
Dim CalcFunction As com.sun.star.uno.XInterface
CalcFunction=createUnoService("com.sun.star.sheet.FunctionAccess")
DateSerialC=CalcFunction.callFunction("DATE", Array(iYear, iMonth,
iDay))
End Function
If not using extended data types, Dim CalcFunction As Object instead, of
course.
Example 3 again:
Print SerialDateA(2021,-73,-4500)
Result: 2002-07-06
Print SerialDateC(2021,-73,-4500)
Result: 2002-07-06
Both match the expected results.
I just find it very strange and inconsistent that SerialDate() is so primitive
compared to the DATE() cell function in Calc. Accepting just about any integer
for month and day is very useful in loops and makes the code more compact and
easier to understand.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are the assignee for the bug.</li>
</ul>
</body>
</html>