Home >Backend Development >Python Tutorial >Python gets the system time (detailed explanation of time function)

Python gets the system time (detailed explanation of time function)

高洛峰
高洛峰Original
2016-10-19 13:44:181393browse

import time

print time.time()

The output result is:

1279578704.6725271

But this is a series of numbers that is not the result we want. We can use the time module's formatting method to process it:

time.localtime(time.time())

Use the time.localtime() method, which formats the timestamp as local time.

The output result is:

time.struct_time(tm_year=2010, tm_mon=7, tm_mday=19, tm_hour=22,

tm_min=33, tm_sec=39, tm_wday=0, tm_yday=200, tm_isdst=0 )

Now it looks more promising to format it to the time we want.

time.strftime('%Y-%m-%d',time.localtime(time.time()))

Finally, use the time.strftime() method to format the large string of information just now into what we want The current result is:

2010-07-19

Time and date formatting symbols in Python:

%y represents a two-digit year (00-99)

%Y represents a four-digit year (000-9999)

%m month (01-12)

%d day in the month (0-31)

%H 24-hour hour (0-23)

%I 12-hour Hours (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 complete month name

%c Local corresponding date representation and time representation

%j Day in the year (001-366)

%p Local equivalent of A.M. or P.M. Symbol

%U The number of weeks in a year (00-53) Sunday is the beginning of the week

%w The week (0-6), Sunday is the beginning of the week

%W The number of weeks in the year (00- 53) Monday is the beginning of the week

%x The corresponding local date representation

%X The corresponding local time representation

%Z The name of the current time zone

%% The % number itself


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn