Basic Training of self-Python datetime

Module of another form of time, and is mainly used to set the time of the extraction unit of time

import datetime
import time
a=datetime.datetime.now()
print('提取当前时间:',a)#获取时间,精确到毫米
print('提取时间单位,年:',a.year)#获取年
print('提取时间单位,月:',a.month)#获取月
print('提取时间单位,日:',a.day)#获取日
print('提取时间单位,小时:',a.hour)#获取小时
print('提取时间单位,分钟:',a.minute)#获取分钟
print('提取时间单位,秒:',a.second)#获取秒

 

Time Size Comparison

Before and after the time, the smaller, the more time, the larger, for example 2

import datetime
import time
a=datetime.datetime.now()
time.sleep(2)
b=datetime.datetime.now()

if b>a:#时间对比
	print('b大于a')
else:
	pass

 

Time increase

import time
import datetime
c=datetime.datetime.now()#获取当前时间
Add_Day=datetime.timedelta(days=1)#根据现在的时间,增加1天
print(c+Add_Day)#计算后的时间

 

Set Point Time

import time
import datetime
a=datetime.datetime(2020,1,13,22,13)
print('设置的时间:',a)
print('现在的时间:',datetime.datetime.now())
while datetime.datetime.now()<a:
	print(datetime.datetime.now())
	print('时间未到,未退出程序')

 

Converting the object to a string:

import time
import datetime
a=datetime.datetime(2020,1,13,15,13)
print('提取并转换为字符串单位年:',a.strftime('%Y'))
print('提取并转换为字符串单位月:',a.strftime('%m'))
print('提取并转换为字符串单位日:',a.strftime('%d'))
print('提取并转换为字符串单位小时',a.strftime('%H'))
print('提取并转换为字符串单位分钟',a.strftime('%M'))

 

 

The string to datetime objects

Meaning that the English word or month to digital

strptime () function

import time
import datetime
a=datetime.datetime.strptime('October 21 2020','%B %d %Y')#前后的格式要一致
print(a)

 

Published 19 original articles · won praise 1 · views 540

Guess you like

Origin blog.csdn.net/gamblerofdestinyR14/article/details/103958207