This article mainly introduces how to get the current time in Python. if you need it, you can refer to the time when I sometimes write a program to use the current time. I want to use python to get the current time, although it is not very difficult, I always forget to lose it once,
To better remember, I wrote down the method for getting the current time today. if you think it is useful to you, you can add it to your favorites.
To obtain time-related information, you need to use the python time Module. The python time module has many very useful functions. you can go to the official
According to the document, if you want to obtain the current time, you need to obtain the timestamp of the current time. the timestamp seems to be the time between January 1, 1970 and now.
You can try the following method to obtain the timestamp of the current time:
Import time
Print time. time ()
The output result is:
1279578704.6725271
However, this is a series of numbers that are not what we want. we can use the time module to format the time:
Time. localtime (time. time ())
The time. localtime () method is used to format the timestamp as the 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 seems that we want the desired time.
Time. strftime ('% Y-% m-% d', time. localtime (time. time ()))
Finally, we used the time. strftime () method to format a large string of information we wanted. The result is:
2010-07-19
Time. strftime has many parameters that allow you to output what you want at will:
The parameters of time. strftime are as follows:
Strftime (format [, tuple])-> string
Returns the specified struct_time (current time by default) according to 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
If you are interested, try it on your own.