top of page
  • John

Get the local date and time including milliseconds

Updated: Aug 31, 2023

This code uses the GetLocalTime Windows API to get a more accurate time than VBA will supply, accurate to around 15 milliseconds. You can also do this using a combination of the VBA Date, Time and Timer functions but doing so runs the risk that the calls to the functions will be out of sync ... you have to call one before the other(s) and the time will have moved on in-between - you run the risk of, for example, getting a date for 'today' using Date then by the time you have called Time to get the time, the actual time may have gone past midnight meaning you get the time for 'tomorrow'. Unlikely but possible.

To avoid the problem, use the GetLocalTime Windows API function. GetLocalTime is one of the simplest Windows API functions - for details, see

As it stands, the code below returns the date and time in a UK format (dd/mm/yy) ... if you want another format, just adjust the order that the elements are added in the ConvertSystemTimeToString procedure e.g. for a US format (mm/dd/yy), swap tSystem.wDay and tSystem.wMonth.

In the declarations section of your module, add the following declarations:

And then add the following functions in the main body of the module:

Example usage:

Which prints, to the Immediate window:

Limitations: Windows only



bottom of page