# 1.使用Python获取系统当前时间
print('---------下面是方法1-----------')
import time
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
print('---------下面是方法2-----------')
import datetime
curr_time = datetime.datetime.now()
print(curr_time)
print(curr_time.date())
print(curr_time.year)
print(curr_time.month)
print(curr_time.day)
print(curr_time.hour)
print(curr_time.minute)
print(curr_time.second)
# 2.时间类型转换
# datetime-->str
# 获取系统当前时间,并转换成字符串类型的指定格式
print('---------datetime转换成str----------')
time_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(time_date)
print(type(time_date))
# str-->datetime
print('---------str转换成datetime----------')
time_str = '2022-01-25 15:59:58'
curr_time = datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
print(curr_time)
print(type(curr_time))
手机扫一扫
移动阅读更方便
你可能感兴趣的文章