`
从此醉
  • 浏览: 1046848 次
  • 性别: Icon_minigender_1
  • 来自: US
社区版块
存档分类
最新评论

Python 入门教程 4 ---- Date and Time

 
阅读更多


第一节

1 介绍得到当前的时间datetime.now()

2 练习

1 设置变量now的值为datetime.now()

2 打印now的值

from datetime import datetime
now = datetime.now()
print now

第二节

1 介绍从datetime.now得到的信息中提取出year,month等

2 练习: 从datetime.now中得到的信息中提取出year,month,day

from datetime import datetime
now = datetime.now()

print now.month
print now.day
print now.year

第三节

1 介绍把输出日期的格式转化为mm//dd//yyyy,我们利用的是+来转化

2 练习:打印当前的日期的格式为mm//dd//yyyy

from datetime import datetime
now = datetime.now()

print str(now.month)+"/"+str(now.day)+"/"+str(now.year)

第四节

1 介绍把输出的时间格式化为hh:mm:ss

2 练习:打印当前的时间的格式为hh:mm:ss

from datetime import datetime
now = datetime.now()

print str(now.hour)+":"+str(now.minute)+":"+str(now.second)

第五节

1 练习:把日期和时间两个连接起来输出

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)



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics