Python script--Gets the current time of the system and the day before

Source: Internet
Author: User
Tags python script timedelta

Python scripts often need to automatically get the current time of the system and the time of the previous day, if in order to obtain the time of the previous day, simply take out the current system time (string type), converted to an integer minus 1, and then converted to a string type, is not feasible, the beginning of the month will appear 0 days, For example, December 1 will become 0 days, and reasonable practice should allow it to automatically push forward one day.


As follows:


1.python to get the current time there is a Times property
Import time
Time_now = Time.strftime ('%y%m%d ')

or Time_now = Time.strftime ("%y%m%d", Time.localtime ())//Gets the current time of year and month day


2. Get yesterday's time datetime
Import datetime
Now_time = Datetime.datetime.now ()
Yes_time = Now_time + Datetime.timedelta (days=-1)

or Yes_time=datetime.datetime.now ()-Datetime.timedelta (Days=1)

Yes_time_yes = yes_time. strftime ('%y%m%d ')//formatted output

This article is from the "one Gourd soy sauce" blog, please be sure to keep this source http://zhushenghang.blog.51cto.com/11105203/1825342

Python script--Gets the current time of the system and the day before

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.