Python Summary of Time operations

Source: Internet
Author: User

1. Get the current system time

Datenow = Datetime.datetime.now ()

2. Convert time in datetime format to STR

Datenow = Datenow.strftime ("%y-%m-%d")

or Datenow = Datetime.datetime.strftime (Datenow, "%y-%m-%d")

3. Convert the time in str format to a DateTime format

Datenow = Datetime.datetime.strptime (Datenow, "%y-%m-%d")

4. A time in a DateTime format known as a forward fetch of a historical date

Import Calendar

① year

For I in Range (1,n):

His_year = Datenow.year-i

His_mon = Datenow.month

his_day = min (Datenow.day, Calendar.monthrange (His_year,his_mon) [1]) #天 cannot exceed the maximum number of days in the month

His_date = Datetime.datetime (His_year, His_mon, His_day)

② Month

For I in Range (1,n):

month = datenow.month-1-I.

His_year = Datenow.year + MONTH/12

His_mon = month%12 + 1

his_day = min (Datenow.day, Calendar.monthrange (His_year,his_mon) [1])

His_date = Datetime.datetime (His_year, His_mon, His_day)

③ days

For I in Range (1,n):

His_date = Datenow-datetime.timedelta (days=i)

Python Summary of Time operations

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.