Summary of how dates and times format output in python

  • 2020-04-02 14:44:10
  • OfStack

This example summarizes how dates and times format output in python. Share with you for your reference. Specific analysis is as follows:

Python format the date/time functions for the datetime. Datetime. The strftime (); Type from string to date function is: datetime. Datetime. Strptime (), two functions involve date/time format string, here to provide a detailed code demonstrates in detail the use of each parameter method and example.

The following is an English abbreviation of the day of the week for the substitution symbol %a used to format a date and time


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%a')
'Sun'

%A outputs the full day of the week in English


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%A')
'Sunday'

% b   Output month


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%b')
'Sep'

%B output month full English name


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%B')
'September'

%c displays the date and time in local time


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%c')
'09/15/13 21:43:29'

%d shows the number between 1 and 31, the day of the month, the day of the month


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%d')
'15'

% H   Display hours on a 24-hour basis, for example, 02,14


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%H')
'21'

%I displays the current hour in a 12-hour format, such as the current jb51.net server at 21:00 PM, and displays 09 using %I


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%I')
'09'

%j shows the current date as the day of the year. If the current jb51.net server time is September 15, 2013, it shows 258, which is the 258th day of the year


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%j')
'258'

%m shows the months between 1 and 12


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%m')
'09'

%M shows the number of minutes between 00 and 59


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%M')
'43'

%p shows morning or afternoon in A.M./ p.m


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%p')
'PM'

%S shows the number of seconds between 0 and 59


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%S')
'29'

%U shows the week of the year, and Sunday is the first day of the week. For example, the current server time of www.jb51.net is September 15, 2013, and Sunday is shown as the 37th week


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%U')
'37'

% w.   Shows the number of days of the week, where Sunday is 0 and Monday is 1. For example: jb51.net the current date is Tuesday, September 17, 2013, and the result is 2


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%w')
'2'

%W shows the week of the year, and U% sets Monday as the first day of the week. For example, the current server time of www.jb51.net is September 17, 2013, Tuesday, and is shown as week 37, ranging from 0 to 51


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%W')
'37'

% x shows the local date, for example jb51.net local time is: Beijing time on September 17, 2013


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%x')
'09/17/13'

% X shows local time, for example jb51.net local time is: Beijing time on September 17, 2013 07:55:04  


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%X')
'07:55:04'

%y shows the years between (00-99), for example: jb51.net server time: September 17, 2013, the result is 13


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%y')
'13'

%Y shows the full year, for example: jb51.net server time: September 17, 2013, the result is 2013


>>> import datetime
>>> now=datetime.datetime.now()
>>> now.strftime('%Y')
'2013'

%z, %z output time zone, if not displayed, displayed as a null character %%   Used to display the % symbol


>>> now.strftime('%%')
'%'

Here's a complete example:
Display current date time: format: year - month - day: minute: second


>>> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S');
'2013-09-17 08:06:17'

I hope this article has helped you with your Python programming.


Related articles: