This article is reproduced from: http://www.cnblogs.com/zhangbo127/p/4548898.html
Two ways to get the current time
Import Datetime,time Now
= Time.strftime ("%y-%m-%d%h:%m:%s")
print now
= Datetime.datetime.now ()
Print Now
Get one hour, day before, previous week, first one months before current time
Import datetime Now
= Datetime.datetime.now ()
# One hour before
D1 = Now-datetime.timedelta (hours=1)
Print D1.strftime ("%y-%m-%d%h:%s:%m")
# the day before
D2 = Now-datetime.timedelta (Days=1)
print d2.strftime ("%y-%m-%d% H:%s:%m ")
# Sunday
d3 = Now-datetime.timedelta (Days=now.isoweekday ())
print d3.strftime ("%y-%m-%d%h:%s:%m ")," ", D3.isoweekday ()
# on Monday
d31 = D3-datetime.timedelta (days=6)
print d31.strftime ("%y-%m-%d%h:%s:% M ")," ", D31.isoweekday ()
# The last day of last month
D4 = Now-datetime.timedelta (days=now.day)
print d3.strftime ("%y-% m-%d%h:%s:%m ")
# First day last month
print datetime.datetime (d4.year, D4.month, 1)
Gets the execution time of the code
Import
Time # You can use a timestamp to get the execution time of the code
StartTime = Time.time () for
I in range (0):
time.sleep (1)
Endtime = Time.time ()
print "time1:", Endtime-starttime
# time.time () and Time.clock () have different results under different operating systems
# Under Ubuntu, Time () Gets the clock past, clock () Gets the CPU execution time on the current process
print (Time.time (), Time.clock ())
Time string turn time stamp, timestamp time string, DateTime object turn timestamp
# string
datestr1 = ' 2015-06-06 10:10:10 '
print ' datestr to time: ', Time.mktime (Time.strptime (DATESTR1, "%y- %m-%d%h:%m:%s "))
# Time Twist format time string
time1 = Time.time ()
print ' Times to Datestr: ', Time.strftime ("%y-%m-%d%h:% M:%s ", Time.localtime (time1))
# DateTime object Turn time rub
datetime1 = Datetime.datetime.now ()
print ' DateTime to Time: ', Time.mktime (Datetime1.timetuple ())
# Timestamp to datetime object
t1 = time.time ()
t2 = t1 +
D1 = Datetim E.datetime.fromtimestamp (t1)
d2 = Datetime.datetime.fromtimestamp (t2)
print ' time1 to datetime1: ', Datetime.datetime.fromtimestamp (t1)
print ' time2 to datetime2: ', Datetime.datetime.fromtimestamp (T2)
print ' Seconds diff: ', (d2-d1). seconds
Time and date formatting symbols description
%y # Two-digit year representation (00-99)
%y # Four-digit year representation (000-9999)
%m # Month (01-12)
%d # month-old (0-31)
%H # 24-Hour system hours (0-23)
%I # 12 hours per hour (01-12)
%m # minutes (00=59)
%s # sec (00-59)
%a # Local Simplified week name%a # Local Full week name%b #
Local Simplified month name
%b # ben The full month name
%c # Local corresponding date representation and time represents
%j # The day of the Year (001-366)
%p # Local a.m. or p.m. Parity
%u # Number of weeks in a year (00-53) Sunday is the beginning of the week
%w # Week (0-6), Sunday for the start of the week
%w # The number of weeks in a year (00-53) Monday is the start of the week
%x # Local corresponding date indicates
%x # Local corresponding time indicates
%Z # The name of the current time zone
% #% per se
Original address:
Http://www.cnblogs.com/zhangbo127/p/4548898.html
Reference Address:
http://wangwei007.blog.51cto.com/68019/1102130
http://www.pythoncentral.io/measure-time-in-python-time-time-vs-time-clock/