python-Foundation-time-Date Processing summary (DateTime module)

Source: Internet
Author: User

When writing code, you will always encounter a variety of time processing and requirements. To summarize:

First of all, this article is a summary of the DateTime module.

Begin.

First, DateTime basic operation

1. Get the current datetime

1 times = datetime.datetime.now ()2 print time3# Output The following date-the hour (accurate to microseconds, 6 digits after decimal point)45 2018-01-11 20:19:34.794000

2. Get Date of day

1 time = datetime.date.today ()2 print time3# The output results are as follows:  45 2018-01-11

3. Get tomorrow/the first n days

Tomorrow

1 time = Datetime.date.today () +datetime.timedelta (Days=1)2print   time 3 # the output is: 4 5 2018-01-12

3 days ago

1 time = Datetime.date.today ()-datetime.timedelta (days=3)2print   time 3 # the output is: 4 5 2018-01-08

Second, the time of the data type conversion

1. DateTime type converted to String type

1 time = Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s')2  Print  time3# output:45 2018-01-11 20:41:23

2. DateTime type is converted to date type (remember this notation, remove date is the exact time, plus date is the day)

1 time = Datetime.datetime.now (). Date ()2print  time3#  The output is:45 2018-01-11

Third, plus timedalta can be calculated in time (days)

Example:

1 time = Datetime.datetime.now (). Date ()-datetime.timedelta (days = 1)  #这里如果不写days默认也是days2  Print  time3# output:45 2018-01-10

End

The end version allows you to view the links below.

This digest from: http://www.wklken.me/posts/2015/03/03/python-base-datetime.html

Python-Foundation-time Date Processing summary (DateTime module)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.