DEV Community

arenasbob2024-cell
arenasbob2024-cell

Posted on • Originally published at viadreams.cc

Unix Timestamps Explained: Convert Between Dates and Epochs

Unix timestamps are the backbone of time handling in computing. Here is everything you need to know about converting between timestamps and dates.

What is a Unix Timestamp?

A Unix timestamp counts the number of seconds since January 1, 1970 00:00:00 UTC (the Unix epoch). Right now, it is around 1,772,000,000.

JavaScript

// Current timestamp (seconds)
Math.floor(Date.now() / 1000)

// Timestamp to Date
new Date(1672531200 * 1000)  // 2023-01-01

// Date to Timestamp
new Date('2023-01-01').getTime() / 1000
Enter fullscreen mode Exit fullscreen mode

Python

import time, datetime

# Current timestamp
int(time.time())

# Timestamp to datetime
datetime.datetime.fromtimestamp(1672531200)

# Datetime to timestamp
int(datetime.datetime(2023, 1, 1).timestamp())
Enter fullscreen mode Exit fullscreen mode

Milliseconds vs Seconds

Source Format Example
Unix/Python Seconds 1672531200
JavaScript Milliseconds 1672531200000
Java Milliseconds 1672531200000L

Common Gotchas

  1. Timezone confusion - timestamps are always UTC
  2. Seconds vs milliseconds - JavaScript uses ms, most others use seconds
  3. Y2K38 problem - 32-bit timestamps overflow on Jan 19, 2038
  4. Daylight saving time - UTC timestamps avoid DST issues entirely

Try It Online

Convert timestamps with DevToolBox Timestamp Converter - supports seconds, milliseconds, multiple date formats.


What timestamp bugs have you encountered? Share below!

Top comments (0)