Date APIs by Language
Date/time API cheatsheet for C, Python, JavaScript, Java, Go, PHP, and SQL.
C standard library
<time.h>
Use
time()
for Unix seconds,
localtime()
/
gmtime()
for
struct tm
, then
strftime()
to format.
time_t now = time(NULL);
struct tm *lt = localtime(&now);
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt);
| Specifier | Meaning | Example |
|---|---|---|
%Y
|
4-digit year | 2026 |
%m
|
Month 01–12 | 05 |
%d
|
Day 01–31 | 23 |
%H
|
Hour (24h) | 16 |
%M
|
Minute | 03 |
%S
|
Second | 00 |
%A
|
Weekday (full) | Saturday |
%z
|
UTC offset | +0800 |
C++11+: use
<chrono>
and
std::put_time
with the same specifiers as
strftime
.
Python
datetime
datetime.now()
for the current moment;
strftime()
to format,
strptime()
to parse strings.
from datetime import datetime, timezone
now = datetime.now()
now.strftime("%Y-%m-%d %H:%M:%S")
datetime.strptime("2026-05-23", "%Y-%m-%d")
datetime.now(timezone.utc).isoformat()
| Code | Meaning | Example |
|---|---|---|
%Y
|
4-digit year | 2026 |
%m
|
Month | 05 |
%d
|
Day | 23 |
%H
|
Hour (24h) | 16 |
%f
|
Microseconds (Python) | 123456 |
%z
|
UTC offset | +0800 |
For time zones prefer
zoneinfo
(3.9+) or
pytz
.
JavaScript
Date
/
Intl
Built-in
Date
for instants; prefer
Intl.DateTimeFormat
for locale output with a
timeZone
option.
const now = new Date();
now.toISOString();
now.toLocaleString("en-US");
new Intl.DateTimeFormat("en-US", {
timeZone: "Asia/Shanghai",
dateStyle: "full",
timeStyle: "medium",
}).format(now);
Math.floor(now.getTime() / 1000);
| API | Meaning |
|---|---|
Date.now()
|
Milliseconds since epoch |
getFullYear()
etc.
|
Local calendar fields |
toISOString()
|
UTC ISO 8601 string |
Intl.DateTimeFormat
|
Format with locale / timeZone |
Temporal
(Stage 3)
|
Next-gen date/time API |
Java
java.time
(Java 8+)
Prefer
Instant
,
ZonedDateTime
, and
DateTimeFormatter
over legacy
Date
/
Calendar
.
import java.time.*;
import java.time.format.DateTimeFormatter;
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
zdt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Instant.now().getEpochSecond();
LocalDate.parse("2026-05-23");
| Pattern | Meaning | Example |
|---|---|---|
yyyy
|
4-digit year | 2026 |
MM
|
Month | 05 |
dd
|
Day | 23 |
HH
|
Hour (24h) | 16 |
mm
|
Minute | 03 |
ss
|
Second | 00 |
XXX
|
Zone offset | +08:00 |
Go
time
package
time.Now()
returns
Time
;
Format
/
Parse
use the reference layout
Mon Jan 2 15:04:05 MST 2006
.
import "time"
now := time.Now()
now.Format("2006-01-02 15:04:05")
time.Parse("2006-01-02", "2026-05-23")
now.Unix()
time.LoadLocation("Asia/Shanghai")
| Layout token | Meaning | Example |
|---|---|---|
2006
|
Year | 2026 |
01
|
Month | 05 |
02
|
Day | 23 |
15
|
Hour (24h) | 16 |
04
|
Minute | 03 |
05
|
Second | 00 |
MST
|
Zone name | CST |
PHP date functions
date()
formats the current time;
DateTime
/
DateTimeImmutable
for object-oriented use.
date("Y-m-d H:i:s");
$dt = new DateTime("now", new DateTimeZone("Asia/Shanghai"));
$dt->format("Y-m-d H:i:s");
DateTime::createFromFormat("Y-m-d", "2026-05-23");
time();
| Specifier | Meaning | Example |
|---|---|---|
Y
|
4-digit year | 2026 |
m
|
Month | 05 |
d
|
Day | 23 |
H
|
Hour (24h) | 16 |
i
|
Minute | 03 |
s
|
Second | 00 |
e
|
Time zone ID | Asia/Shanghai |
SQL date functions
Syntax varies by engine. Examples for MySQL/MariaDB; PostgreSQL uses
TO_CHAR
; SQLite uses
strftime
.
-- MySQL
SELECT NOW();
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s');
SELECT UNIX_TIMESTAMP(NOW());
-- PostgreSQL
SELECT TO_CHAR(NOW(), 'YYYY-MM-DD HH24:MI:SS');
-- SQLite
SELECT strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime');
| Function | Meaning |
|---|---|
NOW() / CURRENT_TIMESTAMP
|
Current date and time |
DATE_FORMAT
(MySQL)
|
Format output |
TO_CHAR
(PostgreSQL)
|
Format output |
strftime
(SQLite)
|
Format (C-like) |
DATEDIFF / DATE_PART
|
Date difference / extract field |