The dates and times are mainly two libraries, datetime and time.
DateTime: Datetime.date.today () Date and time: Datetime.datetime.now () 1000 days later: Datetime.datetimedelta (days=1000) Problem printing format: Isoformat (), strftime () string conversion: Strptime () time:datetime.time (12,11,30) time.time () actual time Time.clock () CPU Time Time.sleep () hibernate
Various do not explain, direct code in practice.
Import module:
>>> Import datetime
Date gets today's dates:
>>> A=datetime.date.today ()
>>> A
Datetime.date (2015, 10, 20)
>>> A.day
20
>>> A.year
2015
>>> A.month
10
DateTime gets today's date:
>>> B=datetime.datetime.now () >>> bdatetime.datetime (2015, 10, 20, 15, 54, 34, 345931)
What time is it after 1000 days:
Timedelta is a special type of object that contains several days and, if necessary, some number of seconds. We can use it to increase or decrease the date.
>>> a=datetime.date.today () >>> D=datetime.timedelta (days=1000) >>> (a+d). Isoformat () ' 2018-07-16 '
Show a better view:
>>> (a+d) strftime ('%m/%d/%y ') ' 07/16/2018 '
What time is it after 1000 hours?
>>> E=datetime.timedelta (hours=1000) >>> B=datetime.datetime.now () >>> (e+b). Isoformat () ' 2015-12-01t08:15:20.952474 '
How many days is it from last year?
>>> sad_day=datetime.datetime.strptime (' 2014-09-15 ', '%y-%m-%d ') >>> B=datetime.datetime.now () >>> print b-sad_day400 days, 16:23:09.111938
See if the clock is back.
>>> Sad_day>bfalse
Set a time to work in advance:
>>> print Datetime.time (12,11,30) 12:11:30
Test the two-stage program which runs faster
Import timea=input ("please input 0 or 1:") start_time = Time.time () Start_clock = Time.clock () If a: sum_i=0 for i in Range (100000): sum_i+=ielse: sum_i=sum (Range (100000)) print Sum_itime.sleep (2) End_time = Time.time () end_ Clock = Time.clock () print "Time-delta:" Print Start_time-end_timeprint "Clock-delta:" Print Start_clock-end_clock
Operation Result:
[email protected] python]# python date.py please input 0 or 1:04999950000time-delta:-2.0064599514clock-delta:-0.01[[ Email protected] python]# python date.py please input 0 or 1:14999950000time-delta:-2.02046704292clock-delta:-0.01
Time.time () The actual time the program is running
Time.clock () CPU time
Time.sleep () Sleep time in seconds
Python Date and time