Get date value from the DateField dialog control

I am new to OO/LO macro. I have created a dialog with DateField and I am setting the value for that field as

dlgObj = CreateUnoDialog(DialogLibraries.Standard.myDialog)
dlgObj.GetControl("date_field_name_value").Text = Now

Later I am trying to get the Date value from this object as

dlgObj.GetControl("date_field_name_value").Text

I am getting the value in String format not a date format. Yes, the property name clearly shows its type Text. But I tried Date, getDate() gives some object but no idea what it is and how to get the date value from this. or any other way. The Date.Month or Date.Year gives value 0.

Please guide me for proper way to set and get the Date value to or from the DateField.

Use:

  oField=dlgObj.GetControl("date_field_name_value")
  oField.Date=CdateToUnoDate(Date())  ' Only date. Now() function returns date and time
  result=CDateFromUnoDate(oField.Date)
1 Like

Thanks, exactly expecting this solution.
Is there any way to check the oField.Date is valid value, because if the user clear the value in the DateField UI then CDateFromUnoDate( oField.Date ) throwing an error. IsNull(), IsEmpty() not working. If I check oField.Text = "" returns true, but if I set the value via macro code also its returns true so even having valid value this method not worked, and this method only return false if manually sets the value in the UI. Currently used on error goto LABEL as workaround for invalid date or empty value in the DateField value in UI.

I recommend reading and re-reading A. Pitonyak’s brilliant book OOME_4_0.odt. (The book was written in 2011, some of its statements may be out of date and not work in current versions of LO.)
In particular, section 18.3.7 contains a description of the Date control. You can set the properties of a control either interactively or programmatically. Pay attention to the properties DateFormat, StrictFormat, DropDown.
If the StrictFormat property is True, then a non-empty field must contain a valid date.

Fwiw, Andrew’s page says that book was last updated June 19, 2018; there’s also his Macro Document, updated June 13, 2015, but best link to his page instead of single editions.

Andrew’s book refers to OpenOffice where date controls and time controls have weird integers such as 20210821 or 234559. LibreOffice uses UNO structs.
[Tutorial] Date-Time Conversion in StarBasic

1 Like

Thanks for the interesting link. Also, not all places in the book (OOME_4_0.odt) take into account the transformation of LO Basic to 32-bit architecture. All this in no way diminishes the value of this grandiose publication.

Thank you for all the response and guidance. In case of empty value in the DateField in UI. If we try to convert using CDateFromUnoDate is throwing error. So I am doing a small validate as oField.Date.Year = 0 is True if the value is empty. With this small check point on error goto LABEL workaround no longer required.

1 Like

This worked, thank you.