Get system time and format output under Python

Source: Internet
Author: User
Tags timedelta

Python has two modules, time and DateTime, which, of course, need to be import first when used.


Get system Current Time Time.localtime (Time.time ())

The formatted output of the time can be used with the strftime, called Time.strftime ()

Example for Current_time=time.strftime ('%y-%m-%d ', Time.localtime (Time.time ()))

The time format for the output is 2015-02-11, and this output is a string type of data


and datatime. Get Time Datetme.datetime.now ()

The call to adjust time in DateTime is Datetime.timedelta ()

For example, get yesterday's date for Datetime.datetime.now () +datetime.timedelta (days=-1)

You want to format the output as yesterday= (Datetime.datetime.now () +datetime.timedelta (Days=-1)). Strftime ('%y-%m-%d ')

The output time format is also 2015-02-10, and this output is also a string type of data.

This article is from the "Learning discussion" blog, so be sure to keep this source http://ltm250.blog.51cto.com/4695970/1613710

Get system time and format output under Python

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.