A simple Timer with dates & times

I would like to share my solution to a simple timer that logs the End date - Startdate = Session time into one row.
It also calculates the totals.

here is a link to the improved sheet: https://www.file-upload.net/en/download-13278487/timelog.ods.html
It auto plots the spent time, the included macro is below

REM  *****  BASIC  *****

Sub MyDateFormat(Cell As Object, NumberFormats As Object, NumberFormatString As String)  

Dim NumberFormatId As Long
Dim LocalSettings As New com.sun.star.lang.Locale  

LocalSettings.Language = "en"
LocalSettings.Country = "us"

NumberFormatId = NumberFormats.queryKey(NumberFormatString, LocalSettings, True)
If NumberFormatId = -1 Then
   NumberFormatId = NumberFormats.addNew(NumberFormatString, LocalSettings)
End If

Cell.NumberFormat = NumberFormatId
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub StartTimer
Dim Doc As Object
Dim Sheet As Object
Dim CurrRow As Integer

Doc = ThisComponent
Sheet = Doc.Sheets.getByName("timer") ' Doc.Sheets(0) 

CellCount = Sheet.getCellByPosition(1, 0)
CellCount.Value = CellCount.Value + 1
CurrRow = CellCount.Value

CellStart = Sheet.getCellByPosition(0, CurrRow) ' ->, v
CellStart.Value = NOW()  
  
'set format
Dim MyFormat As String
MyFormat = "MM/DD/YY HH:MM AM/PM"
MyDateFormat(CellStart, Doc.NumberFormats, MyFormat)
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub EndTimer
Dim Doc As Object
Dim Sheet As Object
Dim CurrRow As Integer

Doc = ThisComponent
Sheet = Doc.Sheets.getByName("timer") ' Doc.Sheets(0) 

CellCount = Sheet.getCellByPosition(1, 0) 
CurrRow = CellCount.Value

CellStart = Sheet.getCellByPosition(0, CurrRow)
CellEnd = Sheet.getCellByPosition(1, CurrRow)
CellEnd.Value = NOW()
 
'insert diff formula
CellSession = Sheet.getCellByPosition(2, CurrRow) 
CellSession.Formula = "= B" + CStr(CellEnd.CellAddress.Row() + 1) + "- A" + CStr(CellStart.CellAddress.Row() + 1)

'set formats
Dim MyFormat As String
MyFormat = "MM/DD/YY HH:MM AM/PM"
MyDateFormat(CellEnd, Doc.NumberFormats, MyFormat)
MyFormat = "HH:MM"
MyDateFormat(CellSession, Doc.NumberFormats, MyFormat)
End Sub
1 Like

Thank you for this handy code snippet @creativeflower
Btw, i think that cell C3 seen in your preview image has a wrong NumberFormat set, to display the duration