Examples of obtaining time using python

  • 2020-04-02 09:47:33
  • OfStack


import time 
print time.time() 
print time.localtime(time.time()) 
print time.strftime('%Y-%m-%d', time.localtime()) 
print time.strftime('%y-%m-%d', time.localtime()) 
print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) 
print time.strftime('%Y-%m-%d %I:%M:%S', time.localtime()) 
print time.strftime('%Y-%m-%d %H:%M:%S --%A--%c', time.localtime()) 
print time.strftime('%Y-%m-%d %H:%M:%S --%x--%X', time.localtime())

Here's the effect:


1295099282.16
time.struct_time(tm_year=2011, tm_mon=1, tm_mday=15, tm_hour=21, tm_min=48, tm_sec=2, tm_wday=5, tm_yday=15, tm_isdst=0)    
2011-01-15
11-01-15
2011-01-15 21:48:02
2011-01-15 09:48:02
2011-01-15 21:48:02 --Saturday--01/15/11 21:48:02
2011-01-15 21:48:02 --01/15/11--21:48:02

The following is the specific parameter description of time.strftime:

Strftime has many parameters that allow you to output what you want more freely:
The following is the parameter of time.strftime:
The strftime (format [, tuple]) - > The string
Outputs the specified struct_time(the default is the current time) according to the specified formatted string
Time and date formatting symbol in python:
The two-digit year of %y is (00-99).
The year of %Y with four digits (000-9999)
Month (01-12)
%d day of the month (0-31)
%H 24-hour hours (0-23)
%I 12 hours (01-12)
%M minutes (00=59)
%S seconds (00-59)

%a locally simplified week name
%A local full week name
%b local simplified month name
%B local full month name
%c local corresponding date and time representations
%j one day in the year (001-366)
Local A.M. or P.M. equivalent of %p
The number of weeks of the year (00-53) Sunday is the beginning of the week
W week (0-6), Sunday is the beginning of the week
The number of weeks in a year (00-53) Monday is the beginning of the week
%x is represented locally by the corresponding date
%X local corresponding time representation
%Z the name of the current time zone
The %% % sign itself


Related articles: