Skip to content

Commit 50f266a

Browse files
committed
新增文件
新增文件
1 parent 601157d commit 50f266a

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

python_basic/main.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Python基础
2+
1. [时间格式转换](时间格式转换.md)

python_basic/时间格式转换.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
* Str-parse-time: “时间字符串”转换成“时间对象”
2+
* Str-format-time: “时间对象”转换成“时间字符串”
3+
## time模块
4+
5+
```python
6+
import time
7+
time.time() # 1529653382.830719
8+
9+
# time.struct_time(tm_year=2018, tm_mon=5, tm_mday=16, tm_hour=10, tm_min=21, tm_sec=2, tm_wday=2, tm_yday=136, tm_isdst=0)
10+
time.localtime()
11+
12+
# 把时间(time.struct_time)转换成指定格式的字符串
13+
# '2018-06-22 15:49:59'
14+
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
15+
16+
# 把时间戳转换为指定格式字符串
17+
time.strftime('%Y%m%d%H%M%S', time.localtime(1529653382.830719))
18+
```
19+
## datetime模块
20+
```python
21+
import datetime
22+
23+
# 把 “指定格式的时间字符串” 转换为“datetime对象”
24+
dt_str = '2018-02-09 07:19:50'
25+
dt_obj = datetime.datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S")
26+
# datetime.datetime(2018, 2, 9, 7, 19, 50)
27+
dt_obj
28+
29+
# 把“datetime对象” 转换为 “指定格式的时间字符串”
30+
# '20180209071950'
31+
dt_obj.strftime("%Y%m%d%H%M%S")
32+
33+
# 把“指定格式的时间字符串” 转换为 “时间戳对象”
34+
dt_obj = datetime.datetime.strptime(s , "%Y-%m-%d %H:%M:%S")
35+
# 1518131990.0
36+
time.mktime(dt_obj.timetuple())
37+
38+
# 把“指定格式的时间字符串” 转换为 “时间戳对象”
39+
# 1518131990.0
40+
time.mktime(time.strptime(s,'%Y-%m-%d %H:%M:%S'))
41+
```

0 commit comments

Comments
 (0)