This article introduces the use of the dateime module to output the date specification format in the Python language:
Program analysis:
#!/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'))
The output result of the above example is:
21/10/2015 05/01/1941 06/01/1941 05/01/1942
The above is the detailed content of Read the full text to tell you how Python outputs the specified format as expected?. For more information, please follow other related articles on the PHP Chinese website!