系统:Windows 7 语言版本:Anaconda3-4.3.0.1-Windows-x86_64 编辑器:pycharm-community-2016.3.2
import datetime
# 转换成字符串
now_time = datetime.datetime.now()
print(now_time)
print(type(now_time))
print('\n')
str_time = now_time.strftime('%Y-%m-%d %H:%M:%S')
print(str_time)
print(type(str_time))
print('\n')
str_time = now_time.strftime('%y %m %d %I-%M-%S %p')
print(str_time)
print(type(str_time))
print('\n')
str_time = now_time.strftime('%B %d %A %j %w')
print(str_time)
print(type(str_time))
print('\n')
代码截图
运行结果
now_time.strftime
,strftime,可以理解为string format的time,即字符串格式的时间,因为后续还会讲一个函数strptime,不要混淆