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
Python code
- #当前时间的时间戳
- 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
Python code
- In [2]: time.localtime ()
- out[2]: Time.struct_time (tm_year=, tm_mon=8, tm_mday=, tm_hour=, tm_min=, tm_sec= tm_wday= 5,tm_yday=222, tm_isdst=0)
Gmtime () and mktime () can freely convert two time representation methods
Python code
- In []: Time.gmtime (Time.time ())
- out[]: Time.struct_time (tm_year=, tm_mon=8, tm_mday=, tm_hour=2, tm_min=, tm_sec= Si , tm_wday=5, tm_yday=222, tm_isdst=0)
- In [23°c]: Time.mktime (Time.localtime ())
- out[]: 1376102845.0
Strftime () can convert the Struct_time type freely into character type
Python code
- in [+]: Time.strftime ("%y%m%d", Time.localtime ())
- out[]: ' 20130810 '
Strptime (string, format) converts a time string to an array of time based on the specified format character
Python code
- in [+]: time.strptime (' 20130810 ', "%y%m%d")
- out[]: Time.struct_time (tm_year=, tm_mon=8, tm_mday=, tm_hour=0, tm_min=0, tm_sec= 0, tm_wday=5, tm_yday=222, tm_isdst=-1)
Two. DateTime module
Python code
- in [+]: Datetime.datetime.now ()
- out[]: datetime.datetime (8, ten, ten, Up, Ten, 611490) /c4>
Python provides several built-in modules for manipulating date times, like Calendar,time,datetime. Time module. The interface of the DateTime module is more intuitive and easier to invoke than the time module.
The DateTime module defines the following classes:
- Datetime.date: A class that represents a date. The commonly used attributes are year, month and day;
- Datetime.time: A class that represents time. The commonly used properties are hour, minute, second, microsecond;
- Datetime.datetime: Represents the DateTime.
- Datetime.timedelta: Represents the time interval, which is the length between two points in time.
- Datetime.tzinfo: Related information about the time zone.
DateTime is a DateTime object that represents the date time
Python code
- in [+]: Datetime.datetime.now ()
- out[]: datetime.datetime (8, ten, ten, Up, Ten, 611490) /c4>
The Strftime method is available in datetime to convert a datetime date to a string:
Java code
- in [+]: Datetime.datetime.now (). Strftime ("%y%m%d")
- out[]: ' 20130810 '
Datetime.strptime (date_string, format): Converts a format string to a DateTime object
Java code
- in [+]: Datetime.datetime.strptime ("20130810", "%y%m%d")
- out[]: datetime.datetime ( 8, 0, 0)
DateTime overloads some operations, which allow us to do some of the following on dates:
- DateTime2 = datetime1 + Timedelta # date plus an interval, returns a new Date object (Timedelta will be described below, representing the time interval)
- datetime2 = datetime1-timedelta # date interval, returns a new Date object
- Timedelta = date1-date2 # Two date subtraction, returns a time interval object
- Datetime1 < DateTime2 # Two dates to compare
Python code
- In []: time1 = Datetime.datetime.now ()
- In [time2]: = Datetime.datetime.now ()
- In []: time2 > Time1
- out[]: True
- In [All]: Datetime.datetime.now ()-Datetime.timedelta (days = 7)
- out[]: datetime.datetime ( 8, 3, one, one, 881810)
Three. Conversion between Time and DateTime
Date.fromtimestamp (timestamp): Returns a Date object based on the given time timestamp
Python code
- In [Wuyi]: Datetime.datetime.fromtimestamp (Time.time ())
- out[]: datetime.datetime ( 8, ten, one, +, 842812) /c4>
The conversion between Time_struct and DateTime can be done by means of an intermediate state string.
--------------------------------------------------------------------------------------------------------------- ------------
Reference documents:
http://blog.csdn.net/jgood/article/details/5457284
http://blog.csdn.net/kiki113/article/details/4033017
Type of time in Python, relationship of datetime type and conversion to each other