Display the date & time a file was created or last modified

    From The Document Foundation Wiki


    Other languages:

    Description

    The function FileDateTime(Text As String) takes file location path as input parameter string, and returns characters containing date and time of creation or last modification of a file. The result if no format is specified is: MM/DD/YYYY HH:MM:SS. For example MsgBox FileDateTime("/home/user1/Documents/foo.odt").

    This function was in fact subject to bug tdf#63306 in some intermediate versions of LibreOffice.

    The LibreOffice API[1] allows you to circumvent this bug and also offers other powerful file access functions.

    The example shown below shows how to retreive the date and time of creation or last modification for a file via the method getDateTimeModified.[2]

    The method takes in a URL, and returns a struct called DateTime[3]. The displaying of the result is acheived by conversion via CDateFromUnoDateTime or by the function Format[4].

    Code

    In LibreOffice Basic:

    Option Explicit
    
    Sub DateTimeInfo()
        '''Display date & time last modified for a specified file'''
    
        Dim oSFA        As Object
        Dim oDateFile   As New com.sun.star.util.DateTime
        Dim sUrl        As String
        
        sUrl = "/home/user1/Documents/foo.odt"
        oSFA = createUnoService("com.sun.star.ucb.SimpleFileAccess")
        
        oDateFile = oSFA.getDateTimeModified(convertToURL(sUrl))
        
        MsgBox CDateFromUnoDateTime(oDateFile),0, "DateTimeInfo()"
        
        With oDateFile
            MsgBox  "Date & time last modified info from" & CHR$(10) & _
                    sURL & " :"  &  CHR$(10) & CHR$(10) & _
                    .year & format(.month, "-00-") & format(.day, "00") & " " & _
                    format(.hours, " \@\ 00") & format(.minutes, "\:00\:") & format(.seconds, "00") _
                    ,0, "DateTimeInfo()"
        End With
        
    End Sub ' DateTimeInfo()

    Example ODT file to test macro

    Notes

    1. The LibreOffice API (Application Programming Interface) allows interaction with various programming languages.
    2. See the API documentation for the XSimpleFileAccess interface.
    3. See the API documentation for the DateTime struct
    4. See the documentation for the Format function