Summary of methods for date and time formatted output in Python _python

Source: Internet
Author: User
This article mainly introduces the method of date and time formatted output in Python, the example summarizes Python common date and event operation skill, it is very practical value, the friend who need can refer to the following

This article summarizes the methods of date and time formatted output in Python. Share to everyone for your reference. The specific analysis is as follows:

Python formats a datetime function as Datetime.datetime.strftime (), a function that converts a string into a date type: Datetime.datetime.strptime (), and two functions that involve a DateTime formatted string. Detailed code detailing the use and example of each parameter is provided here.

The following is the alternate symbol that is available when formatting the date and time%a output is currently the day of the week in English shorthand


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


%A output full weekday name in English


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


%b in English for the output month


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


%B Full English name of the output month


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


%c displays 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 of 1-31, the day ordinal of a month,


>>> 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 12-hour mode, such as the current jb51.net server at 21 o'clock, using%i 09


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


%J Displays the current date as the day of the year, such as the current Jb51.net server time of September 15, 2013, which is displayed as 258, which is 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 shows the number of minutes between 00-59


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


%p to a.m./p.m. The way is morning or afternoon


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


%s shows the number of seconds between 0-59


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


%u displays the week 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, which is shown as the 37th week


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


%w Displays the day of the 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 week of the year, and u% the difference is Monday for the first day of the week, for example the current Www.jb51.net server time is September 17, 2013, Tuesday, shown as 37th week, range between 0-51


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


%x Displays the local date, for example jb51.net local time: Beijing time 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: 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 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 server time is: September 17, 2013, displaying a result of 2013


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


%z,%z output time zone, if not displayed, display as null character percent used to display% symbol


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


To give a complete example:
Show current datetime: Format: Year-month-day time: minutes: seconds


>>> Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s '); ' 2013-09-17 08:06:17 '
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.