JavaScript
Datestores milliseconds internally;getTime()andDate.now()are ms.Date.parseaccepts ISO strings but not Unix numbers.Intl.DateTimeFormatis the reliable way to format a specific timezone.
// 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.
utcfromtimestampreturns a naive value and is deprecated in 3.12.fromisoformatgainedZsupport in Python 3.11.
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
Instantis the modern API;java.util.Dateis legacy and stores milliseconds.LocalDateTimehas no zone and is a common source of off-by-hours bugs.- Use
ZoneIdnames, not raw offsets, when DST matters.
// 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.RFC3339is the constant for ISO 8601 with an offset.- Always call
.UTC()before logging anInstant-like value.
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
-- 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.