Python 入门教程 4 ---- Date and Time
第一节
1 介绍得到当前的时间datetime.now()
2 练习
1 设置变量now的值为datetime.now()
2 打印now的值
[python]
<span style="font-size:18px">from datetime import datetime
now = datetime.now()
print now</span>
第二节
1 介绍从datetime.now得到的信息中提取出year,month等
2 练习: 从datetime.now中得到的信息中提取出year,month,day
[python]
<span style="font-size:18px">from datetime import datetime
now = datetime.now()
print now.month
print now.day
print now.year</span>
第三节
1 介绍把输出日期的格式转化为mm//dd//yyyy,我们利用的是+来转化
2 练习:打印当前的日期的格式为mm//dd//yyyy
[python]
<span style="font-size:18px">from datetime import datetime
now = datetime.now()
print str(now.month)+"/"+str(now.day)+"/"+str(now.year)</span>
第四节
1 介绍把输出的时间格式化为hh:mm:ss
2 练习:打印当前的时间的格式为hh:mm:ss
[python]
<span style="font-size:18px">from datetime import datetime
now = datetime.now()
print str(now.hour)+":"+str(now.minute)+":"+str(now.second)</span>
第五节
1 练习:把日期和时间两个连接起来输出
[python]
<span style="font-size:18px">from datetime import datetime
now = datetime.now()
print str(now.month) + "/" + str(now.day) + "/" + str(now.year) + " "\
+str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)</span>
补充:Web开发 , Python ,