JavaScript

  • Date stores milliseconds internally; getTime() and Date.now() are ms.
  • Date.parse accepts ISO strings but not Unix numbers.
  • Intl.DateTimeFormat is the reliable way to format a specific timezone.
javascript
// now
Date.now()                          // milliseconds
Math.floor(Date.now() / 1000)       // seconds

// parse seconds -> Date
new Date(seconds * 1000)

// format as ISO (UTC)
new Date(seconds * 1000).toISOString()

// local display
new Date(seconds * 1000).toLocaleString('en-US', { timeZone: 'Asia/Shanghai' })

Python

  • Prefer timezone-aware datetimes; naive datetimes are ambiguous.
  • utcfromtimestamp returns a naive value and is deprecated in 3.12.
  • fromisoformat gained Z support in Python 3.11.
python
from datetime import datetime, timezone

# now
ts = int(datetime.now(timezone.utc).timestamp())

# seconds -> aware datetime
dt = datetime.fromtimestamp(ts, tz=timezone.utc)

# format
dt.isoformat()          # 2025-11-16T14:00:00+00:00
dt.strftime('%Y-%m-%d') # date only

# parse ISO
# Python 3.11+: datetime.fromisoformat('2025-11-16T14:00:00Z')

Java

  • Instant is the modern API; java.util.Date is legacy and stores milliseconds.
  • LocalDateTime has no zone and is a common source of off-by-hours bugs.
  • Use ZoneId names, not raw offsets, when DST matters.
java
// now
Instant now = Instant.now();
long seconds = now.getEpochSecond();
long millis  = now.toEpochMilli();

// seconds -> Instant
Instant i = Instant.ofEpochSecond(seconds);

// format
DateTimeFormatter f = DateTimeFormatter.ISO_INSTANT;
f.format(i);

// zoned
ZonedDateTime z = i.atZone(ZoneId.of("Asia/Shanghai"));

Go

  • time.Unix(sec, nsec) takes seconds and nanoseconds, not milliseconds.
  • time.RFC3339 is the constant for ISO 8601 with an offset.
  • Always call .UTC() before logging an Instant-like value.
go
now := time.Now()
sec := now.Unix()          // seconds
ms  := now.UnixMilli()     // milliseconds

// seconds -> time.Time
t := time.Unix(sec, 0).UTC()

// parse ISO
parsed, err := time.Parse(time.RFC3339, "2025-11-16T14:00:00Z")

// format
parsed.Format(time.RFC3339)

SQL

sql
-- PostgreSQL
SELECT extract(epoch from now())::bigint;          -- seconds
SELECT to_timestamp(1763311200);                   -- timestamptz
SELECT to_char(to_timestamp(1763311200), 'YYYY-MM-DD');

-- MySQL
SELECT UNIX_TIMESTAMP();                           -- seconds
SELECT FROM_UNIXTIME(1763311200);                  -- datetime in session tz

MySQL's TIMESTAMP type is 32-bit and cannot represent dates after 2038-01-19 in many builds. Use DATETIME for long-lived data.