Home  >  Article  >  Backend Development  >  Read the full text to tell you how Python outputs the specified format as expected?

Read the full text to tell you how Python outputs the specified format as expected?

Tomorin
TomorinOriginal
2018-08-17 14:46:461174browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn