0%

时间戳转换方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
datetime = datetime.datetime.now()
timestamp = time.time()
datetime_format = %Y-%m-%d %H:%M:%S

# str -> datetime
datetime.datetime.strptime(string, datetime_format)

# datetime -> str
datetime.datetime.strftime(datetime_format)
type(a) -> datetime.datetime
a.strftime(datetime_format)

# datetime -> timestamp
time.mktime(datetime.datetime.timetuple())

# timestamp -> datetime
datetime.datetime.fromtimestamp(timestamp)
datetime.datetime.fromtimestamp(timestamp).strftime(datetime_format)

# timestamp -> time
time.localtime(timestamp)