Summary of Date and Time formatting output methods in python, python Summary

Source: Internet
Author: User

Summary of Date and Time formatting output methods in python, python Summary

This example summarizes the date and time formatting methods in python. Share it with you for your reference. The specific analysis is as follows:

The function for formatting the date and time in python is datetime. datetime. strftime (); Convert from string to date type function: datetime. datetime. strptime (). Both functions involve the format string of the date and time. The detailed code here demonstrates the usage of each parameter and its example.

The following is a replacement symbol available for formatting the date and time. % a is short for the current day of the week.

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

% A: name of the complete day of the week in English

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

% B

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

% B Name of the output month in English

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

% C display date and time with local time

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

% D indicates the number between 1-31, the day of the month, that is, the day of the year, month, and day.

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

% H displays the hour in the 24-hour format, for example ,.

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

% I displays the current hour in 12-hour format. For example, if the current jb51.net server time is, use % I to display 09

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

% J displays the day of the year as the current date. For example, if the current jb51.net server time is January 1, 258, it indicates the day of the year.

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

% M displays the month from 1 to 12

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

% M indicates the number of minutes between-59.

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

% P is displayed in A. M./P. M. morning or afternoon

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

% S display the number of seconds between 0 and 59

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

% U indicates the week of the year, and Sunday is the first day of the week. For example, the current www.jb51.net server time is January 1, 37th, and Sunday.

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

% W indicates the day of the week, where Sunday is 0 and Monday is 1. For example, if the current jb51.net date is Tuesday September 17, 2013, the result is 2.

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

% W shows the week of the year, and U % sets the day of the week as the first day of the week. For example, the current www.jb51.net server time is January 1, 37th, and Tuesday, the value range is between 0 and 51.

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

% X shows the local date. For example, jb51.net local time is: January 1, September 17, 2013, Beijing time.

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

% X shows the local time. For example, jb51.net local time is: 07:55:04, January 1, September 17, 2013, Beijing time.

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

% Y indicates the year between 00 and 99. For example, if jb51.net server time is July 15, September 17, 2013, the result is 13.

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

% Y indicates the full year. For example, if the jb51.net server time is July 22, September 17, 2013, the result is 2013.

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

% Z, % Z output time zone. If it cannot be displayed, It is a null character % used to display the % symbol

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

Here is a complete example:
Display current date and time: Format: year-month-day hour: minute: Second

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

I hope this article will help you with 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.