Python Formats the time date

Source: Internet
Author: User
Tags locale month name

>>> Import datetime>>> dir (datetime) [' Maxyear ', ' minyear ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __ package__ ', ' Date ', ' datetime ', ' datetime_capi ', ' time ', ' Timedelta ', ' Tzinfo ']>>> datetime.datetime.now () Datetime.datetime (2015, 5, 11, 17, 29, 14, 476759)

Python Formats the date-time function as datetime.datetime.strftime ();

The function of converting from string to date is:datetime.datetime.strptime (), two functions all involve datetime formatted string, listed as follows:


%a

Abbreviated weekday name


%A

Full weekday name

%b

Abbreviated month name

%B

Full month name

%c

Date and time representation appropriatefor 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 the 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


To give an example:

The time format in ebay is ' sep-21-09 16:34 '

The string is converted to datetime using the following code

>>> C = datetime.datetime.strptime (' sep-21-09 16:34 ', '%b-%d-%y%h:%m ');>>> Cdatetime.datetime (2009 , 9, 21, 16, 34)


Another example: DateTime conversion to a string

>>> Datetime.datetime.now (). Strftime ('%b-%d-%y%h:%m:%s ') ' may-11-15 17:29:29 '

Python Formats the time date

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.