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

Tomorin
Release: 2018-08-17 14:46:46
Original
1287 people have browsed it

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'))
Copy after login

The output result of the above example is:

21/10/2015
05/01/1941
06/01/1941
05/01/1942
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template