Matlab Epoch Time Converter: How to Use It
- Unix timestamp (seconds)
- 1609459200
- You entered
This page explains how to convert between Unix epoch time (seconds since 1970-01-01 00:00:00 UTC) and human-readable dates in MATLAB, and provides a simple calculator to do the conversion for you. You enter a Unix timestamp (or a date string), and you get the corresponding date/time in your local time zone or UTC, depending on your MATLAB settings. The calculator below handles the core arithmetic for the most common case: converting a Unix timestamp to a UTC date-time string.
Formula
MATLAB does not have a built-in function named epoch, but it provides several ways to convert epoch time. The most direct method uses the fact that MATLAB’s date number for 1970-01-01 00:00:00 UTC is 719529 (in MATLAB’s serial date number system, where day 1 is 0000-01-01). To convert a Unix timestamp (seconds since 1970-01-01 UTC) to a MATLAB datenum, you add the offset and divide by the number of seconds per day:
datenum_utc = (unix_time / 86400) + 719529
Where:
unix_time– seconds since 1970-01-01 00:00:00 UTC (can be negative for dates before 1970).86400– seconds in one day.719529– MATLAB datenum for 1970-01-01 00:00:00 UTC.
Then you can convert that datenum to a readable date string using datestr(datenum_utc, 'yyyy-mm-dd HH:MM:SS') or to a datetime object with datetime(datenum_utc, 'ConvertFrom', 'datenum', 'TimeZone', 'UTC').
Worked Example
Using the default value from the calculator: timestamp = 1609459200 (which is 2021-01-01 00:00:00 UTC).
- Divide by 86400: 1609459200 / 86400 = 18628.0.
- Add the offset: 18628.0 + 719529 = 738157.0.
- That datenum corresponds to 2021-01-01 00:00:00 UTC.
The calculator shows the result as a decimal number (738157.000000) because that is the raw datenum. In MATLAB, you would then use datestr(738157.0) to get ’01-Jan-2021 00:00:00′.
How to Convert in MATLAB: Built-in Functions
MATLAB provides several built-in functions that make epoch conversion straightforward:
posixtime(datetime)– converts adatetimeobject to Unix time (seconds since 1970-01-01 00:00:00 UTC). This is the reverse of what you need.datetime(unix_time, 'ConvertFrom', 'posixtime')– converts a Unix timestamp to adatetimeobject. This is the easiest method in modern MATLAB (R2014b and later).datenumanddatestr– older functions that work with serial date numbers.
Example: t = 1609459200; dt = datetime(t, 'ConvertFrom', 'posixtime'); gives a datetime object in UTC. To display it in your local time zone, use dt.TimeZone = 'local'; (or set the ‘TimeZone’ parameter when creating).
Choosing the Right Method
Which method you should use depends on your MATLAB version and your needs:
- R2014b or later: Use
datetimewith'ConvertFrom', 'posixtime'. It handles time zones, leap seconds, and fractional seconds automatically. - R2014a or earlier: Use the datenum offset method (as shown in the formula) because
datetimedid not exist yet. - If you need a string output: Use
datestron the datenum result, or usechar(datetime(...)).
For batch conversions (e.g., a vector of timestamps), the datetime function works element-wise, so you can pass an array directly.
Common Pitfalls and Misconceptions
Several issues trip up users:
- Time zone confusion: Unix time is always UTC. If you want local time, you must specify the time zone.
datetime(t, 'ConvertFrom', 'posixtime', 'TimeZone', 'local')does that. - Milliseconds vs. seconds: Some systems use milliseconds (e.g., JavaScript
Date.now()). If you feed a millisecond value into a seconds-based converter, you get a date in 1970. Divide by 1000 first. - Leap seconds: Unix time ignores leap seconds; MATLAB’s
posixtimealso ignores them. This is consistent with POSIX standard. - Negative timestamps: Dates before 1970 produce negative numbers. The conversion formula still works, but be careful with integer division.
- Datenum offset: A common mistake is using 719529 vs. 719529.0; both work, but using integers can cause rounding errors for large timestamps. Use double precision.
Alternatives to MATLAB for Epoch Conversion
If you are not tied to MATLAB, many other tools can convert epoch time:
- Excel: Excel’s date system starts on 1900-01-01, so the offset is 25569 days. Formula:
=(A1/86400)+25569, then format as date. - Python:
datetime.datetime.fromtimestamp(t)(local) ordatetime.datetime.utcfromtimestamp(t)(UTC). - Online converters: Many websites offer instant conversion, but they may not handle time zones or milliseconds correctly.
- Command line: On Linux/macOS,
date -d @1609459200works.
MATLAB’s advantage is that it integrates with your data analysis pipeline, so you can convert timestamps directly in your script without leaving the environment.
How to Read the Result
When you convert a Unix timestamp, the result is a date-time string or a datetime object. If you see a decimal number like 738157.0, that is a MATLAB serial date number, not a human-readable date. Always convert it to a string or datetime object for readability. The calculator above outputs the raw datenum for transparency, but in practice you would use datestr or datetime to format it.
For example, datestr(738157.0) returns ’01-Jan-2021 00:00:00′. If you want ISO 8601 format, use datestr(738157.0, 'yyyy-mm-ddTHH:MM:SS').
Frequently Asked Questions
How do I convert a Unix timestamp to local time in MATLAB?
Use datetime(t, 'ConvertFrom', 'posixtime', 'TimeZone', 'local'). This will display the time in your system’s time zone. If you want a specific time zone, replace ‘local’ with e.g., ‘America/New_York’.
Why do I get a date in 1970 when I convert a large number?
You are likely using milliseconds instead of seconds. Divide the number by 1000 first. For example, 1609459200000 ms is actually 1609459200 seconds.
Does MATLAB handle leap seconds?
No, MATLAB’s posixtime and datetime functions ignore leap seconds, consistent with the POSIX standard. This is usually fine for most applications.
Can I convert a date string to Unix time in MATLAB?
Yes, use posixtime(datetime('2021-01-01 00:00:00', 'TimeZone', 'UTC')). This returns the Unix timestamp in seconds.
What is the difference between datenum and datetime?
datenum returns a double number representing days since 0000-01-01, while datetime is an object that stores date and time with time zone support. datetime is recommended for new code.
How do I convert a vector of timestamps?
Simply pass the vector to datetime: datetime([1609459200, 1609545600], 'ConvertFrom', 'posixtime'). It works element-wise.
Related
Formula used
in t | Unix timestamp (seconds) | - | 1609459200 | 0 out dt | UTC date-time | - | (t / 86400) + 25569 | 6
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.