Python Date and time

Source: Internet
Author: User
Tags timedelta

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.