Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in MATLAB

🎈 🎈 🎈
+1

    % Convert Date to Unix timestamp
    date = datetime(2007, 12, 27, 19, 28, 12); % Create a datetime object
    unixTimestamp = posixtime(date);
    
    disp(['Unix Timestamp: ', num2str(unixTimestamp)])            
        

Output:
Unix Timestamp: 1198783692
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this example of datetime to Unix timestamp conversion, a datetime object is created for the date '2007-12-27 19:28:12' using the datetime constructor.
The posixtime() function converts the datetime object to the corresponding Unix timestamp in seconds since January 1, 1970. The result, '1198783692', is then printed.



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