Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Unix Timestamp to Datetime in MATLAB

How to convert Unix timestamp to Datetime in MATLAB

🎈 🎈 🎈
+1

    % Convert Unix timestamp to Date
    unixTimestamp = 1231919995; % Unix timestamp (seconds since 1970)
    date = datetime(unixTimestamp, 'ConvertFrom', 'posixtime');
    
    disp(['Converted Date: ', datestr(date)])            
        

Output:
Converted Date: 2009-01-14 07:59:55
Example only. There may be multiple ways to perform this operation.

Code Explanation

To convert Unix timestamp to date in MATLAB, the Unix timestamp 1231919995 is converted to a datetime object using datetime(unixTimestamp, 'ConvertFrom', 'posixtime').
The posixtime option tells MATLAB to interpret the number as seconds since January 1, 1970. The result is then converted to a readable string using datestr() and displayed, in this case 2009-01-14 07:59:55



Other useful MATLAB date functions

Function Description Example
datetime() Creates a datetime object for the specified date and time. datetime(2020, 5, 15, 8, 30, 45) returns a datetime object for 2020-05-15 08:10:45
posixtime() Converts a datetime object to Unix timestamp (seconds since 1970). posixtime(datetime(2020, 5, 15, 8, 30, 45)) returns 1589530245
datestr() Converts a datetime object to a human-readable string. datestr(datetime('now')) returns a formatted string of the current date and time.
now() Returns the current date and time as a serial date number. now() returns the current date and time in serial number format.
datenum() Converts a date string or datetime object to a serial date number. datenum('2020-05-15 08:10:45') returns a serial date number.
datevec() Converts a serial date number or date string to a date vector [year, month, day, hour, minute, second]. datevec(now()) returns the current date as a vector.
year() Extracts the year from a datetime object or serial date number. year(datetime('now')) returns the current year.
month() Extracts the month from a datetime object or serial date number. month(datetime('now')) returns the current month.
day() Extracts the day of the month from a datetime object or serial date number. day(datetime('now')) returns the current day of the month.
hour() Extracts the hour from a datetime object or serial date number. hour(datetime('now')) returns the current hour.
minute() Extracts the minute from a datetime object or serial date number. minute(datetime('now')) returns the current minute.
second() Extracts the second from a datetime object or serial date number. second(datetime('now')) returns the current second.
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder