15):题目:输出指定格式的日期。

    程序分析:使用 datetime 模块。

    程序源代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*- import datetime if __name__ == '__main__': # 输出今日日期,格式为 dd/mm/yyyy。更多选项可以查看 strftime() 方法
print(datetime.date.today().strftime('%d/%m/%Y')) # 创建日期对象
miyazakiBirthDate = datetime.date(1941, 1, 5) print(miyazakiBirthDate.strftime('%d/%m/%Y')) # 日期算术运算
miyazakiBirthNextDay = miyazakiBirthDate + datetime.timedelta(days=1) print(miyazakiBirthNextDay.strftime('%d/%m/%Y')) # 日期替换
miyazakiFirstBirthday = miyazakiBirthDate.replace(year=miyazakiBirthDate.year + 1) print(miyazakiFirstBirthday.strftime('%d/%m/%Y'))

    以上实例输出结果为:

21/10/2015
05/01/1941
06/01/1941
05/01/1942
#!/usr/bin/python
# -*- coding: UTF-8 -*- import time print time.time() #1498539133.655
print time.localtime() #tm_year=2017, tm_mon=6, tm_mday=27, tm_hour=12, tm_min=53, tm_sec=16, tm_wday=1, tm_yday=178, tm_isdst=0
print time.asctime() #'Tue Jun 27 12:53:50 2017'
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) #'2017-06-27 13:00:57' import datetime print datetime.date.today() #datetime.date(2017, 6, 27)
print datetime.date.today().strftime('%d/%m/%Y') #'27/06/2017'
print datetime.date(1941, 1, 5) #datetime.date(1941, 1, 5)
#!/usr/bin/python
# -*- coding: UTF-8 -*- from datetime import *
import time #返回当前本地时间
print("today:"+str(date.today()))
print("today:"+str(date.fromtimestamp(time.time())))

    python 3 的时间:

import time

print(time.time())
print(time.localtime())
print(time.asctime())
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) import datetime print(datetime.date.today())
print(datetime.date.today().strftime('%d/%m/%Y'))
print(datetime.date(1941, 1, 5))

    这个小例子倒是挺有趣的。。。

    如果感觉不错的话,请多多点赞支持哦。。。

  原文链接:https://blog.csdn.net/luyaran/article/details/80049929

05-11 20:18