>>> Import datetime>>> dir (datetime) [' Maxyear ', ' minyear ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __ package__ ', ' Date ', ' datetime ', ' datetime_capi ', ' time ', ' Timedelta ', ' Tzinfo ']>>> datetime.datetime.now () Datetime.datetime (2015, 5, 11, 17, 29, 14, 476759)
Python Formats the date-time function as datetime.datetime.strftime ();
The function of converting from string to date is:datetime.datetime.strptime (), two functions all involve datetime formatted string, listed as follows:
%a |
Abbreviated weekday name |
|
%A |
Full weekday name |
|
%b |
Abbreviated month name |
|
%B |
Full month name |
|
%c |
Date and time representation appropriatefor locale |
|
%d |
Day of month as decimal number (01-31) |
|
%H |
Hour in 24-hour format (00-23) |
|
%I |
Hour in 12-hour format (01-12) |
|
%j |
Day of the year as decimal number (001-366) |
|
%m |
Month as decimal number (01-12) |
|
%M |
Minute as decimal number (00-59) |
|
%p |
Current locale ' s a.m./p.m. indicator for 12-hour clock |
|
%s |
Second as decimal number (00-59) |
|
%u |
Week of year as decimal number,with Sunday as first day of Week (00-51) |
|
%w |
Weekday as decimal number (0-6; Sunday is 0) |
|
%W |
Week of year as decimal number,with Monday as first day of Week (00-51) |
|
%x |
Date representation for current locale |
|
%x |
Time representation for current locale |
|
%y |
Year without century, as decimal number (00-99) |
|
%Y |
Year with century, as decimal number |
|
%z,%z |
Time-zone name or abbreviation; No characters if time zone is unknown |
|
%% |
Percent Sign |
|
To give an example:
The time format in ebay is ' sep-21-09 16:34 '
The string is converted to datetime using the following code
>>> C = datetime.datetime.strptime (' sep-21-09 16:34 ', '%b-%d-%y%h:%m ');>>> Cdatetime.datetime (2009 , 9, 21, 16, 34)
Another example: DateTime conversion to a string
>>> Datetime.datetime.now (). Strftime ('%b-%d-%y%h:%m:%s ') ' may-11-15 17:29:29 '
Python Formats the time date