Python time to get the formatting time

Source: Internet
Author: User

Python time to get the formatting time

To obtain time-related information in Python, you need to use the python time module, which has many very useful functions.

Used in Pythontime.time()Obtain the timestamp of the current time:

>>> import time>>> print time.time()1438608599.0

The printed time here is the time between January 1, 1970 and the present. Such a series of numbers are not what we want. We can use the time module to format the time. Usetime.localtime()Format the timestamp as the local time.

>>> time.localtime(time.time())time.struct_time(tm_year=2015, tm_mon=8, tm_mday=3, tm_hour=21, tm_min=30, tm_sec=59, tm_wday=0, tm_yday=215, tm_isdst=0)

Now it seems that we want the desired time. Usetime.strftime()Method To Format a large string of information into what we want. The result is:

>>> time.strftime('%Y-%m-%d',time.localtime(time.time()))'2015-08-03 '>>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))'2015-08-03 21:32:12'

time.strftime()There are many parameters that allow you to output what you want at will:
Below istime.strftime()Parameters:

strftime(format[, tuple]) -> string

Returns the specified struct_time (current time by default) based on the specified formatted string.

Time and date formatting symbols in python:
% Y two-digit year (00-99)
% Y indicates the four-digit year (000-9999)
% M month (01-12)
One day in % d month (0-31)
% H hour in 24-hour format (0-23)
% I 12-hour (01-12)
% M minutes (00 = 59)
% S seconds (00-59)

% A local simplified week name
% A local full week name
% B local simplified month name
% B local full month name
% C local Date and Time
% J one day in the year (001-366)
% P local equivalent of A. M. or P. M.
% U number of weeks in a year (00-53) Sunday is the start of the week
% W Week (0-6), Sunday is the beginning of the week
% W number of weeks in a year (00-53) Monday is the start of the week
% X local date Representation
% X Local Time Representation
% Z Current Time Zone name
% Itself

 

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.