Excerpt from: http://my.oschina.net/935572630/blog/397557
Http://www.runoob.com/python/python-date-time.html
Date class
The date class represents a day. The date is made up of the year, Month and day (the Earth knows ~ ~). The constructor for the date class is as follows:
Class Datetime.date (year, Month, day):
Date.today (): Returns a Date object that represents the current local date;
# Date.today (): 2010-04-06
Date.weekday (): Returns weekday, if it is Monday, returns 0 if it is Week 2, returns 1, and so on;
Data.isoweekday (): Returns weekday, if it is Monday, returns 1 if it is Week 2, returns 2, and so on;
Date.strftime (FMT): Custom formatted string. Explained in detail below.
Time class
The time class represents times, consisting of hours, minutes, seconds, and microseconds. (I'm not from Mars.) The constructor for the time class is as follows:
Class Datetime.time (hour[, minute[, second[, microsecond[, Tzinfo]]): The meaning of each parameter is not explained, note here the parameter tzinfo, which represents the time zone information. Note the range of values for each parameter: The range of Hour is [0], the range of minute is [0], the range of second is [0, 0], and the range of microsecond is [1000000].
class properties defined by the time class:
The minimum and maximum time that the Time.min, Time.max:time class can represent. wherein, Time.min = time (0, 0, 0, 0), Time.max = time (23, 59, 59, 999999);
Time.resolution: The smallest unit of time, here is 1 microseconds;
The time class provides instance methods and properties:
Time.hour, Time.minute, Time.second, Time.microsecond: Hours, minutes, seconds, microseconds;
Time.tzinfo: time zone information;
Time.replace ([hour[, minute[, second[, microsecond[, Tzinfo]]]]): Creates a new time object with the time, minutes, seconds, and microseconds specified by the parameter instead of the attributes in the original object (the original object remains unchanged);
Time.isoformat (): A string representation of the return type, such as "HH:MM:SS" format;
Time.strftime (FMT): Returns a custom formatted string. described in detail below;
DateTime class
DateTime is a combination of date and time, including all information about date and time. Its constructor is as follows: Datetime.datetime (year, month, day[, hour[, minute[, second[, microsecond[, Tzinfo] []]), the meaning of each parameter with date, As in the constructor of time, be aware of the range of parameter values.
The class properties and methods defined by the DateTime class:
Datetime.today (): Returns a DateTime object representing the current local time;
# Today (): 2010-04-07 09:48:16.234000
DateTime.Now ([TZ]): Returns a DateTime object that represents the current local time, and if the parameter tz is provided, gets the local time of the time zone referred to by the TZ parameter;
Datetime.strptime (date_string, format): Converts a format string to a DateTime object
Datetime.year, month, day, hour, minute, second, microsecond, Tzinfo:
Datetime.date (): Gets the Date object;
Datetime.time (): Gets the time object;
format string
datetime, date, and time all provide the strftime () method, which receives a format string that outputs a string representation of the DateTime.
Python format symbols in time Date:
- %y Two-digit year representation (00-99)
- %Y Four-digit year representation (000-9999)
- %m Month (01-12)
- One day in%d months (0-31)
- %H 24-hour hours (0-23)
- %I 12-hour hours (01-12)
- %M minutes (00=59)
- %s seconds (00-59)
- %a Local Simplified Week name
- %A Local Full week name
- %b a locally simplified month name
- %B Local Full month name
- %c Local corresponding date representation and time representation
- %j Day of the Year (001-366)
- %p the equivalent of a local a.m. or p.m.
- %u weeks of the year (00-53) Sunday is the beginning of the week
- %w Week (0-6), Sunday for the beginning of the week
- %W Week of the Year (00-53) Monday is the beginning of the week
- %x Local corresponding date representation
- %x Local corresponding time representation
- %Z the name of the current time zone
- Percent% of the number itself
Example:
[python] view plaincopy
DT = DateTime.Now ()
-
print ' (%y-%m-%d %h:%m:%s %f): ' , Dt.strftime (%y-%m-%d %h:%m:%s % F '
-
print ' (%y-%m-%d %h:%m:%s %p): ' , Dt.strftime (%y-%m-%d %i:%m:%s % P '
-
print '%%a: %s ' '%a '
-
print '%%a: %s ' '%A '
-
print '%%b: %s ' '%b '
-
print '%%b: %s ' '%B '
-
print ' date Time%%c: %s ' '%c '
-
print ' date%%x:%s '
-
print ' time%%x:%s ' '%x '
-
print ' Today is this week's%s day '
-
print ' Today is this year's%s day '
Print ' This week is this year's%s week ' % dt.strftime ('%u ')
# #----Results----
# (%y-%m-%d%h:%m:%s%f): 2010-04-07 10:52:18 937000
# (%y-%m-%d%h:%m:%s%p): 10-04-07 10:52:18 AM
#%a:wed
#%a:wednesday
#%B:APR
#%b:april
# Date Time%C:04/07/10 10:52:18
# date%X:04/07/10
# time%x:10:52:18
# today is the 3rd day of the Week #
# today is the No. 097 day of the Year #
# This week is the 14th week of this year
Date and Time module