Storing epoch time value as a datetime class

6 views (last 30 days)
Cameron Barry
Cameron Barry on 1 Apr 2021
Answered: Soumya on 12 Jun 2025
Is there a way to store an epoch time in seconds since 2000-01-01 as a datetime data type? I see you can insert this epoch time as a double and convert TO any format such as 'yyyy-DDD hh:mm:ss.SSSSSS' using datetime but I need to store the epoch time as a datetime. I have considered using convertTo when converting a datetime with specific format to an epoch time but it returns the value as an integer. The most important part of this is that I maintain microsecond precision. Any suggestions would be greatly appreciated!

Answers (1)

Soumya
Soumya on 12 Jun 2025
Yes, it is possible to store epoch time values measured in seconds as MATLAB datetime objects while preserving microsecond precision. MATLAB’s datetime class supports conversion from numeric epoch time values using the 'ConvertFrom' and 'Epoch' name-value pair arguments in the constructor. By specifying a custom epoch using the 'Epoch' argument, you can also define the reference date from which the numeric epoch times are measured. You can set the 'Format' property to control how the datetime is displayed and show precision to microsecond level.
The following code snippet demonstrates how to achieve the conversion from epoch time values to datetime objects with microsecond precision:
  • Define the custom epoch reference:
customEp = datetime(2000, 1, 1, 0, 0, 0);
  • Convert the epoch value to datetime using microsecond precision:
dt = datetime(epVal, 'ConvertFrom', 'epochtime', 'Epoch', customEp,'Format', 'yyyy-MM-dd HH:mm:ss.SSSSSS');
The following resources will provide more information on the concept of converting epoch value to datetime:
I hope this helps!

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!