Python-time conversion

Source: Internet
Author: User
Use the datetime module to convert fromdatetimeimporttimedeltaatimedelta (days2, hours6) btimedelta (hours4.5) ca + bprint (c. days) print (c. seconds) print (c. seconds3600) indicates the specified date and time... & quot; use the datetime module to convert different time units

Python code

from datetime import timedelta    a = timedelta(days=2, hours=6)  b = timedelta(hours=4.5)  c = a + b  print(c.days)  print(c.seconds)  print(c.seconds / 3600)


Indicates a specific date and time

Java code

from datetime import datetime  a = datetime(2016, 8, 30)  print(a + timedelta(days=10))  b = datetime(2016, 9, 30)  d = b - a  print(d.days)    now = datetime.today()  print(now)  print(now + timedelta(minutes=10))

The datetime module can correctly process the leap year.

Java code

a = datetime(2012, 3, 1)  b = datetime(2012, 2, 28)  print((a-b).days)  c = datetime(2013, 3, 1)  d = datetime(2013, 2, 28)  print((c-d).days)

The dateutil module can be used to deal with more complex date problems, such as processing time zones, blurring time ranges, and calculating holiday dates.

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.