Current epoch — epochtime.tools · A precision instrument for time

Get the current Unix timestamp

import pandas as pd
pd.Timestamp.now(tz='UTC').timestamp()   # seconds, float
pd.Timestamp.now(tz='UTC').value         # nanoseconds, int

Convert a Unix timestamp to a date

df = pd.DataFrame({'ts': [1700000000, 1700003600]})
df['when'] = pd.to_datetime(df['ts'], unit='s', utc=True)
# 0   2023-11-14 22:13:20+00:00

pd.to_datetime(df['ts_ms'], unit='ms', utc=True)   # milliseconds

Convert a date to a Unix timestamp

epoch = pd.Timestamp('1970-01-01', tz='UTC')
df['ts'] = (df['when'] - epoch) // pd.Timedelta('1s')   # int seconds
df['ts_ms'] = (df['when'] - epoch) // pd.Timedelta('1ms')

This is the method the pandas documentation recommends. It gives the right answer whatever resolution the column uses.

Timezones

df['when_ist'] = df['when'].dt.tz_convert('Asia/Kolkata')
# naive local times: attach the zone first, then convert
df['local'] = pd.to_datetime(df['local_str']).dt.tz_localize('America/New_York').dt.tz_convert('UTC')

Common mistakes

  • Without utc=True, results are naive (no zone). Mixing naive and aware columns raises errors when you compare them.
  • In pandas 2 and later, datetime columns can be stored in s, ms, us or ns. .astype('int64') returns the count in that unit, which is not always nanoseconds. Subtracting the epoch avoids the problem.
  • tz_localize raises on times that don't exist or happen twice at DST changes. Pass nonexistent= and ambiguous= to choose.

Complete example

import pandas as pd

df = pd.DataFrame({'ts': [1700000000, 1794650400]})
df['utc'] = pd.to_datetime(df['ts'], unit='s', utc=True)
df['tokyo'] = df['utc'].dt.tz_convert('Asia/Tokyo')
df['back'] = (df['utc'] - pd.Timestamp('1970-01-01', tz='UTC')) // pd.Timedelta('1s')
print(df)

Check any value with the epoch converter, or convert a whole column with the batch converter. All examples use 1700000000 (2023-11-14 22:13:20 UTC) and 1794650400 (2026-11-14 10:00:00 UTC).

Official documentation

Last updated: