标签:for rda 识别 sign Oday second 时间 time pre
import datetime
from datetime import datetime
datetime.now()
datetime.datetime(2018, 8, 1, 22, 50, 40, 408121)
‘{dt:%y-%m-%d}‘.format(dt = datetime.now()) # 格式化
‘18-08-01‘
f‘{datetime.now():%Y-%m-%d}‘
‘2018-08-01‘
from datetime import date
d = date(2019,3,3) # shift +tab
print(d)
2019-03-03
date.today()
datetime.date(2018, 8, 1)
from datetime import time
t = time(2,4)
‘‘‘
Init signature: time(self, /, *args, **kwargs)
Docstring:
time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object
‘‘‘
‘\nInit signature: time(self, /, *args, **kwargs)\nDocstring: \ntime([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object\n‘
print(t)
02:04:00
# 时间中的各种运算
from datetime import timedelta
d = timedelta(days = 1 , hours =4)
print(d)
1 day, 4:00:00
today = date.today()
print(today)
2018-08-01
future = today + d # 加法
print(future)
2018-08-02
future = today + d * 5 # 乘法
print(future)
2018-08-06
today = date.today()
print(type(today))
today.day
print(today.month)
<class ‘datetime.date‘>
8
count = future - today
print(count)
5 days, 0:00:00
type(count)
datetime.timedelta
count.days # 间隔多少天
5
# 从时间戳生成datetime
import time
t = time.time()
print(t)
1533135872.8751283
from datetime import datetime
datetime.utcfromtimestamp(t)
datetime.datetime(2018, 8, 1, 15, 4, 32, 875128)
##############
# 格式化字符串表示时间
# strftime : $-> 1999-12-12 把代码里面的时间对象转换成人类识别的字符串
# strptime: 2000-02-23 -> object 把人类识别的字符串转换成代码里面的对象
from datetime import datetime
dt = datetime(2018,2,3,4)
print(dt)
2018-02-03 04:00:00
dt.strftime(‘%Y-%m-%m %X‘)
‘2018-02-02 04:00:00‘
dt.strftime(‘%A %B %Y‘)
‘Saturday February 2018‘
datetime.strptime(‘Saturday February 2018‘, ‘%A %B %Y‘) # 原路返回
datetime.datetime(2018, 2, 1, 0, 0)
# 下周的星期四是几号
from datetime import datetime
from dateutil.relativedelta import relativedelta
from dateutil.rrule import * # MO TU WE TH FR
d = datetime.now()
print(d)
2018-08-01 23:16:20.587947
print(d + relativedelta(weekday=FR))
2018-08-03 23:16:20.587947
print(d + relativedelta(weekday=FR,weeks= +1)) # 下周五是几号的用法
2018-08-10 23:16:20.587947
print(d + relativedelta(weekday=FR,weeks= -1)) #上周五是几号的用法
2018-07-27 23:16:20.587947
# 过一个月后,是几号
from datetime import date
date(2018,3,23) + relativedelta(months=+1)
datetime.date(2018, 4, 23)
# 过一个月后,是几号
from datetime import date
date(2018,3,3) + relativedelta(months=+2)
datetime.date(2018, 5, 3)
# 过一个月后,是几号
from datetime import date
date(2018,3,3) + relativedelta(months=-10)
datetime.date(2017, 5, 3)
标签:for rda 识别 sign Oday second 时间 time pre
原文地址:http://blog.51cto.com/13118411/2153385