forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmydatetime.py
More file actions
39 lines (28 loc) · 825 Bytes
/
mydatetime.py
File metadata and controls
39 lines (28 loc) · 825 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
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: sample
Desc :
"""
from datetime import datetime, date, time, timedelta
def unix_time(dt):
epoch = datetime.utcfromtimestamp(0)
delta = dt - epoch
return delta.total_seconds()
def unix_time_millis(dt):
return '{:.0f}'.format(unix_time(dt) * 1000.0)
def my_datetime():
today = datetime.now()
print(today.ctime())
oneday = timedelta(days=1)
tomorrow = today + oneday
print(tomorrow.ctime())
# str to date
dt = datetime.strptime('2012-01-12 12:12:12', '%Y-%m-%d %H:%M:%S')
# date to str
print(dt.strftime('%Y-%m-%d %H:%M:%S'))
if __name__ == '__main__':
tt = datetime(2014, 12, 31, 12, 42, 50)
print(unix_time_millis(tt))
# tt = datetime(2015, 10, 12)
# print(unix_time_millis(tt))