The difference and connection between Python time and datetime

Source: Internet
Author: User

One. Time module

Time module provides functions for various operating times
There are generally two ways to represent time:
The first is the way the timestamp is (relative to the offset from 1970.1.1 00:00:00 in seconds), the timestamp is unique

#当前时间的时间戳 in [9]: Time.time () out[9]: 1376102328.536908

The second is represented as an array of (Struct_time), a total of nine elements, respectively, that the same timestamp of the struct_time will vary depending on the time zone

In [2]: Time.localtime () out[2]: Time.struct_time (tm_year=2013, tm_mon=8, tm_mday=10, tm_hour=10, tm_min=30, tm_sec=47, Tm_wday=5, tm_yday=222, tm_isdst=0)

 gmtime () With Mktime () two time representations can be freely converted

In []: Time.gmtime (Time.time ()) out[22]: Time.struct_time (tm_year=2013, tm_mon=8, tm_mday=10, tm_hour=2, tm_min=46, tm_sec=54, Tm_wday=5, tm_yday=222, tm_isdst=0) in [23°c]: Time.mktime (Time.localtime ()) out[23]: 1376102845.0


Strftime () can convert the Struct_time type freely into character type

In []: Time.strftime ("%y%m%d", Time.localtime ()) out[24]: ' 20130810 '


Strptime (string, format) converts a time string to an array of time based on the specified format character

in [+]: time.strptime (' 20130810 ', "%y%m%d") out[26]: Time.struct_time (tm_year=2013, tm_mon=8, tm_mday=10, tm_hour=0, Tm_min=0, Tm_sec=0, tm_wday=5, tm_yday=222, Tm_isdst=-1)

Note that the timestamp cannot be converted directly to a string, and the time string cannot be converted directly to a timestamp, but only through localtime, using the Mktime method to turn


Two. DateTime module

in [+]: Datetime.datetime.now () out[27]: Datetime.datetime (2013, 8, 10, 10, 56, 10, 611490)


Strftime method, you can convert a datetime date to a string

in [+]: Datetime.datetime.now (). Strftime ("%y%m%d") out[28]: ' 20130810 '


Datetime.strptime (date_string, format): Converts a format string to a DateTime object

in [+]: Datetime.datetime.strptime ("20130810", "%y%m%d") out[30]: Datetime.datetime (2013, 8, 10, 0, 0)


Three. Calculate the first 1 days of the current time

One_day_before = Datetime.datetime.today ()-Datetime.timedelta (Days=1)

Day: Days

Hours: Hours

Minutes: Minutes


Four. Conversion between Time and DateTime

Time to DateTime

A = Time.time () datetime.datetime.fromtimestamp (a)

DateTime converted to TIME

b = Datetime.datetime.now () time.mktime (B.timetuple ())



Reference: http://iam42.iteye.com/blog/1922875

http://blog.csdn.net/hong201/article/details/3193121

This article is from the "Bulajunjun" blog, make sure to keep this source http://5148737.blog.51cto.com/5138737/1760592

The difference and connection between Python time and datetime

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.