Reference blog:
Http://www.cnblogs.com/lhj588/archive/2012/04/23/2466653.html
Http://www.cnblogs.com/fclbky/articles/4098204.html
Reference: Python 2.7.7 Documentation
Reference tool: http://translate.google.cn/
Available Types:
Class Datetime.date idealized date
Class Datetime.time idealized time, assuming every day is 24*60*60 seconds (no leap second concept)
Class Datetime.datetime contains date and time
Class Datetime.timedelta A time interval between the expression of a date, an hour, and a DateTime instance.
The abstract base class for the class Datetime.tzinfo time zone information object, used for the datetime and time classes to provide custom timing concepts (for example, time zones and summer solstice).
First, date Objects
Class Datetime.date (year, month, day)
1) class methods:
Date.today () returns the current time
>>> Date.today ()
Datetime.date (2015, 6, 23)
Date.fromtimestamp (timestamp) Returns a Date object based on the given time timestamp;
>>> Date.fromtimestamp (4564656)
Datetime.date (1970, 2, 23)
>>> Date.fromtimestamp (Time.time ())
Datetime.date (2015, 6, 23)
Date.fromordinal (ordinal) converts the Gregorian calendar time to a Date object;
>>> date.fromordinal (1)
Datetime.date (1, 1, 1)
>>> date.fromordinal (2000)
Datetime.date (6, 6, 23)
2) Class attributes:
Date.min/max/resolution/year/month/day
>>> Date.max
Datetime.date (9999, 12, 31)
Frequently used instances of Year/month/day:
>>> Date1=date.today ()
>>> Date1
Datetime.date (2015, 6, 23)
>>> Date1.year
2015
3) Instance methods:
Date.replace (year, month, day) generates a new Date object, substituting the attributes in the original object with the years, months, and days specified by the parameter. (The original object remains unchanged)
Date.timetuple (): Returns the Time.struct_time object corresponding to the date;
>>> Date1.timetuple ()
Time.struct_time (tm_year=2015, tm_mon=6, tm_mday=23, tm_hour=0, tm_min=0, tm_sec
=0, Tm_wday=1, tm_yday=174, Tm_isdst=-1)
Date.toordinal (): Returns the date corresponding to the Gregorian calendar date;
Date.weekday (): Return the day of the week as a integer, where Monday is 0 and Sunday is 6.
Date.isoweekday (): Return the day of the week as a integer, where Monday is 1 and Sunday is 7.
Date.isocalendar (): Returns a tuple with a format such as (Year,month,day)
Date.isoformat (): Returns a string formatted as ' YYYY-MM-DD '
DATE.__STR__ (): For a Date D, str (d) are equivalent to D.isoformat ().
>>> Str (DATE1)
' 2015-06-23 '
>>> Date1.isoformat ()
' 2015-06-23 '
>>> date1.__str__ ()
' 2015-06-23 '
Date.ctime () returns a string in a specific format
>>> Date1.ctime ()
' Tue June 23 00:00:00 2015 '
Date.strftime (format): Custom formatted string, format specific rules see Strftime () and Strptime () Behavior
DATE.__FORMAT__ (format): Same as Date.strftime ().
Second, Time Objects
Class Datetime.time ([hour[, minute[, second[, microsecond[, Tzinfo]] []]
1) Class attributes
Time.min/max/resolution
2) Instance attributes (read-only):
Time.hour/minute/second/microsecond/tzinfo
3) Instance Methods
Time.replace ([hour[, minute[, second[, microsecond[, Tzinfo]] []]
Time.isoformat ()
TIME.__STR__ ()
Time.strftime (format)
TIME.__FORMAT__ (format)
There are tzinfo useful methods (see the Python manual example): If Tzinfo is None, returns none.
Time.utcoffset ()
TIME.DST ()
Time.tzname ()
Third, DateTime Objects (Appears to be: datetime = date + time)
Class Datetime.datetime (year, month, day[, hour[, minute[, second[, microsecond[, Tzinfo] []])
1) class methods
Datetime.today ()
DateTime.Now ([TZ]): Return the current local date and time,if optional argument tz are None or not specified, this is a like T Oday ().
Datetime.utcnow (): Return the current UTC date and time, with Tzinfo None.
Datetime.fromtimestamp (timestamp[, TZ]): Creates a DateTime object based on timestamp, parameter TZ specifies time zone information
Datetime.utcfromtimestamp (timestamp)
Datetime.fromordinal (ordinal)
Datetime.combine (date, time): Returns a DateTime object, combined with date and time;
★datetime.strptime (date_string, format): Converts a format string to a DateTime object
2) Class attributes
Datetime.min/max/resolution
3) Instance attributes (read-only)
Datetime.year/month/day/hour/minute/second/microsecond/tzinfo
4) Instance Methods
Datetime.date (): Gets the Date object
Datetime.time (): Gets the time object, excluding Tzinfo
Datetime.timetz (): Gets the time object, including the Tzinfo
Datetime.replace ([year[, month[, day[, hour[, minute[, second[, microsecond[, Tzinfo]])
Datetime.astimezone (TZ) passes in a new Tzinfo property, returning a DateTime object that is adjusted according to the new time zone
There are ways to use Tzinfo:
Datetime.utcoffset ()
DATETIME.DST ()
Datetime.tzname ()
Datetime.timetuple ()
Datetime.utctimetuple ()
Datetime.toordinal ()
Datetime.weekday ()
Datetime.isoweekday ()
Datetime.isocalendar (): (ISO year, ISO week number, ISO weekday).
DATETIME.__STR__ ()
Datetime.isoformat ([sep= ' T ']): Returns a date string in ISO 8601 format, with the date and time connected with Sep
>>> datetime.today (). Isoformat (")
' 2015-06-23 15:01:05.008000 '
>>> datetime.today (). Isoformat (' | ')
' 2015-06-23|15:01:11.875000 '
Datetime.ctime ()
Datetime.strftime (format)
DATETIME.__FORMAT__ (format)
Iv. Timedelta Objects
Class Datetime.timedelta ([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, Weeks]] []]
1) Class attributes
Timedelta.min/max/resolution
2) Instance attributes (read-only)
Attribute Value
Days Between-999999999 and 999999999 inclusive
Seconds between 0 and 86399 inclusive
microseconds between 0 and 999999 inclusive
3) Instance Methods
Timedelta.total_seconds ()
Five, Tzinfo Objects (temporarily do not write, see later)
六、一个 Little Practice
‘‘‘
Give two recognizable dates and calculate the number of days between two date intervals.
Input: STR1,STR2 format is YYYY/MM/DD, as in 2013/08/06, no validation required for entry
return: type int, with positive negative number, when the date str1 is expressed before str2, the return value is less than 0
‘‘‘
#!-*-Coding:utf-8-*-
Import datetime
Class Demo:
def intervaldays (self, str1, str2):
Data1 = Datetime.datetime.strptime (str1, "%y/%m/%d")
Data2 = Datetime.datetime.strptime (str2, "%y/%m/%d")
Delta = data1-data2
Return delta.days
if __name__ = = ' __main__ ':
A=demo ()
Print a.intervaldays (' 2013/08/06 ', ' 2012/08/06 ')
Python Standard library-datetime Learning