Epoch & Unix Timestamp in C

Get the current epoch timestamp

#include <time.h>

time_t epoch_seconds = time(NULL);

Convert epoch to date

time_t ts = 1718200000;
struct tm *utc = gmtime(&ts);
char buf[32];
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", utc);

Convert date to epoch

struct tm tm = {0};
tm.tm_year = 2026 - 1900; tm.tm_mon = 5; tm.tm_mday = 12;
time_t epoch = timegm(&tm);

C notes

tm_year is years since 1900 and tm_mon is 0-based — both classic bugs. timegm is POSIX (not ISO C); mktime is its local-time sibling. Ensure time_t is 64-bit to survive 2038.

Check any value live with the epoch converter, or read what Unix time actually counts.

Other languages

C# · C++ · Dart · Go · Java · JavaScript · Kotlin · MySQL · Perl · PHP · PostgreSQL · Python · R · Ruby · Rust · Scala · SQLite · Swift · TypeScript