-
Notifications
You must be signed in to change notification settings - Fork 3
/
use_datetime.py
33 lines (24 loc) · 994 Bytes
/
use_datetime.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta, timezone
#datetime.now()返回当前日期和时间
now = datetime.now()
print('now=', now)
print('type(now)=', type(now))
dt = datetime(2017, 11, 27, 11, 25)
print('dt = ', dt)
print('datetime -> timestamp:', dt.timestamp())
t = dt.timestamp()
print('timestamp -> datetime:', datetime.fromtimestamp(t))
print('timestamp -> datetime as UTC+0:', datetime.utcfromtimestamp(t))
cday = datetime.strptime('2017-6-1 18:59:10', '%Y-%m-%d %H:%M:%S')
print('strptime', cday)
print('strftime', cday.strftime('%a, %b %d %H:%M'))
print('current datatime=', cday)
print('current + 10 hours =', cday+timedelta(hours=10))
print('current - 1 day =', cday-timedelta(days=1))
print('current + 2.5 day =', cday+timedelta(days=2,hours=12))
utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)
utc8_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))
print('UTC+0:00 now=', utc_dt)
print('UTC+8:00 now=', utc8_dt)