How to format the output in Python date and time summary _python

Source: Internet
Author: User
Tags local time

The example in this article summarizes the methods for formatting the date and time in Python. Share to everyone for your reference. The specific analysis is as follows:

The Python format date time function is Datetime.datetime.strftime (), and the function converted from a string to a date type is: Datetime.datetime.strptime (), and two functions involve a formatted string of DateTime. This provides detailed code detailing how each parameter is used and an example.

The following are the alternate symbols available when formatting dates and times%a output is currently the day of the week

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

%A output full weekday number of English

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

%b Output Month in English

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

%B the English full name of the output month

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

%c displays date and time as local time

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

%d shows the number between 1-31, the first days of the month, the day of the year

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

%H displays hours in a 24-hour system, 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 time of the current jb51.net server, at 21 o'clock, using%i display 09

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

%J Displays the current date as the first day of the year, such as September 15, 2013 for the current Jb51.net server time, and 258 for the No. 258 day of the year

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

%m shows the month between 1-12

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

%M displays the number of minutes between 00-59

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

%p to a.m./p.m. Way to show the morning or the afternoon

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

%s Displays the number of seconds between 0-59

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

%u displays the week ordinal of the year, Sunday is the first day of the week, for example, the current Www.jb51.net server time is September 15, 2013, Sunday, and 37th week

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

%w Displays the day ordinal of a week, where Sunday is 0, Monday is 1, for example: Jb51.net The current date is September 17, 2013 Tuesday, the result is 2

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

%w shows the first few weeks of the year, and u% the difference is Monday for the day of the week, for example, the current Www.jb51.net server time is September 17, 2013, Tuesday, display as week 37th, range between 0-51

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

%x displays local dates, such as Jb51.net local time: Beijing time September 17, 2013

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

%x displays local time, such as Jb51.net local time: Beijing time September 17, 2013 07:55:04

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

%Y Displays the year between (00-99), for example: Jb51.net The server time is: September 17, 2013, the result is 13

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

%Y Displays the full year, for example: Jb51.net The server time is: 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, then display as a null character% to display% symbol

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

To give a complete example:
Show current date Time: Format: Year-month-day: minutes: sec

>>> Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s ');
2013-09-17 08:06:17 '

I hope this article will help you with your Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.