forked from happyjared/python-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmytime.py
More file actions
31 lines (17 loc) · 651 Bytes
/
Copy pathmytime.py
File metadata and controls
31 lines (17 loc) · 651 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from datetime import datetime
def now_date():
return datetime.now()
def now_year():
return datetime.now().year
def now_str():
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def date_to_str(date_time):
return date_time.strftime('%Y-%m-%d %H:%M:%S')
def str_to_date(str_time):
return datetime.strptime(str_time, '%Y-%m-%d %H:%M:%S')
def str_to_date_with_format(str_time, str_format):
return datetime.strptime(str_time, str_format)
def timestamp_to_datetime(timestamp):
if len(str(timestamp)) == 13:
timestamp = timestamp / 1000 # 毫秒时间戳转秒
return datetime.fromtimestamp(timestamp)