Epoch and Date Time Conversion in Matlab

Matlab (matrix laboratory) is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. It allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages. It also provides many date time functions to handle date time functionality.

Here we will explain Matlab date time functions to get current epoch or Unix timestamp, convert timestamp to date and convert date to epoch or Unix timestamp.


Get current epoch or Unix timestamp in Matlab

You can get the current unix timestamp using following.

int32(floor(60*60*24 * (datenum(now) - datenum('01-Jan-1970')))) * 1000

Convert epoch or Unix timestamp to date in Matlab

You can convert unix timestamp to human readable date using following.

unixTimestamp = 1624705077;
date = datestr(unixTimestamp/86400 + datenum(1970,1,1));

Convert date to epoch or unix timestamp in Matlab

You can unix timestamp from human readable date using following.

int32(floor(86400 * (datenum('04-June-2021') - datenum('01-Jan-1970'))))



More about date time in Matlab