https://en.wikipedia.org/wiki/ISO_8601
- Linux
- https://man7.org/linux/man-pages/man3/strftime.3.html
- https://linux.die.net/man/3/strftime
- C,Ruby,Python,PHP 等语言都是同一套百分号格式化方法(https://www.strfti.me/),和上面的 Linux 手册中的一致,其实就是 C 标准。
- C
- https://cplusplus.com/reference/ctime/strftime/
- Python
- https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
- https://docs.python.org/3/library/time.html#time.strftime
- https://strftime.org/
- https://pyformat.info/
- PHP
- https://www.php.net/manual/en/function.strftime.php
- C
- Golang 比较另类
- https://www.geeksforgeeks.org/time-formatting-in-golang/
占位符
占位符 | 描述 |
---|---|
%Y |
四位数的年份 |
%m |
两位数的月份 |
%d |
两位数的日数 |
%H |
24 小时制的小时数 |
%M |
两位数的分钟数 |
%S |
两位数的秒数 |
%z |
时区偏移量,格式如 +0800 |
%a |
星期几的缩写,例如:Mon、Tue 等 |
%A |
星期几的全称,例如:Monday、Tuesday 等 |
%b |
月份的缩写,例如:Jan、Feb 等 |
%B |
月份的全称,例如:January、February 等 |
%c |
本地日期时间,例如:Tue Aug 16 21:30:00 1988 |
%f |
微秒,范围是 0~999999 |
%j |
年份中的第几天,范围是 001~366 |
%p |
上午或下午,例如:AM、PM |
%r |
12 小时制的时间,例如:09:30:00 PM |
%s |
自 1970 年 1 月 1 日以来的秒数 |
%u |
星期几,范围是 1~7,其中 1 表示星期一 |
%w |
星期几,范围是 0~6,其中 0 表示星期日 |
%x |
本地日期,例如:08/16/88 |
%X |
本地时间,例如:21:30:00 |
%y |
两位数的年份,例如:88 |
%Z |
时区名称或缩写,例如:UTC、GMT、EST 等 |
Linux
date -u +"%Y-%m-%dT%H:%M:%S.%3NZ"
Python
import datetime
now = datetime.datetime.now()
iso8601 = now.isoformat(timespec='milliseconds')
print(iso8601)
PHP
$now = new DateTime();
$iso8601 = $now->format('Y-m-d\TH:i:s.v\Z');
echo $iso8601;
Java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String iso8601 = now.format(formatter);
System.out.println(iso8601);
JS
const now = new Date();
const iso8601 = now.toISOString();
console.log(iso8601);
C
#include <stdio.h>
#include <time.h>
int main() {
char iso8601[30];
time_t now = time(NULL);
strftime(iso8601, sizeof iso8601, "%Y-%m-%dT%H:%M:%S.000Z", gmtime(&now));
printf("%s\n", iso8601);
return 0;
}
Golang
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now().UTC()
iso8601 := now.Format("2006-01-02T15:04:05.000Z")
fmt.Println(iso8601)
}