The function for formatting the date and time in python is datetime. datetime. strftime (); Convert from string to date type function: datetime. datetime. strptime (), two functions are involved in the format string of the date and time, as shown below:
% A abbreviated weekday name
% A full weekday name
% B abbreviated month name
% B full month name
% C Date and Time Representation appropriate for 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 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
For example:
The time format in ebay Is 'sep-21-09'
Use the followingCodeConvert the string to datetime.
>>> C = datetime. datetime. strptime ('sep-21-09 34 34 ',' % B-% d-% Y % H: % m ');
>>> C
Datetime. datetime (2009, 9, 21, 16, 34)
Another example: Convert datetime to string
>>> Datetime. datetime. Now (). strftime ('% B-% d-% Y % H: % m: % s ');
'Sep-22-09 16:48:08'