Python time stamp and format time conversion implementation code

Source: Internet
Author: User
The time-related modules in Python are mostly times and datetime

If you want to get the system current timestamp: Time.time (), is a float type of data
Get system Current Time information: Time.ctime () is a str type of time string, generally less used and in development
If you want to get the current normal date string, you can simply use STR (datetime.date.today ())

There is the conversion between time and time stamps (very often):

Conversion on date-to-timestamp:

Import Datetimeimport Timet = Datetime.datetime (2014,12, 6, A, ten, ten) timestamp = Time.mktime (T.timetuple ()) Print Timest Amp


Timestamp to time-date conversion:

Import Datetimeimport Timet = Time.localtime (timestamp) timestr = Time.strftime ('%y-%m-%d%h:%m:%s ', t) print Timestr

One of the more common scenarios is:
Depending on the day of the week or the next few days
For example, I need to know 10 days of data since 2014-10-25.
In this case, we need to perform time calculations, if by nature, the transition between timestamps and time.
We can get the timestamp of 2014-10-25 by time-to-timestamp, then perform the operation on the timestamp, then
Then the result of the operation is converted into a time string, the complete code is as follows:

Import Timeimport Datetimet = Datetime.datetime (all, ten, 3600) timestamp = Time.mktime (t.timetuple ()) Timestamp + = 10 * 24t = time.localtime (timestamp) timestr = Time.strftime ('%y-%m-%d%h:%m:%s ', T)

In fact, the date module has a function to encapsulate this algorithm, the trial is not so large code, but we only talk about the principle, the process of Python time above
In fact, for any language should be such a process

Water sentence: Language tools are constantly changing, you can not cling to a language, not outdated only algorithms and thinking

Related Article

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.