Python3 datetime and time to get the current date and times

Source: Internet
Author: User
Tags timedelta

Some of the time-related packages and functions are most used and later used to supplement

Import datetime Import Time # Gets the current period, which contains year, month, hour, which requires import datetime today = Datetime.date.today () print (Toda
Y) print (today.year) print (today.month) print (today.day) ' >>>2017-01-01 >>>2017 >>>1 >>>1 ' ' # to get tomorrow, and the other in turn tomorrow = Today + Datetime.timedelta (Days=1) print (Tomorrow) ' >>>2017-01-0
2 ' ' # time subtracted, adding empathy now = Datetime.timedelta (days=0, hours=0, minutes=3, seconds=50);

Pre = Datetime.timedelta (days=0, hours=0, Minutes=1, seconds=10); Duration_sec = (now-pre). Seconds Duration_day = (now-pre). Days Print (Type (duration_sec)) print (Type (now-pre)) print (
DURATION_SEC) print (duration_day) ' >>><class ' int ' > >>><class ' Datetime.timedelta ' > >>>160 >>>0 ' ' # uses time.strftime (format, p_tuple) to get the current time, which requires import times now = Time.strftime ("%h:%m:%s" Print (now) ' ' >>>23:49:34 ' # using DateTime.Now () now = Datetime.datetime.now () print (now) print (now.year) PRI NT (now. Month) print (now.day) print (now.hour) print (now.minute) print (now.second) print (now.microsecond) ' >>>
2017-01-01 23:49:34.789292 >>>2017 >>>1 >>>1 >>>23 >>>49 >>>34 >>>789292 ' "
Document Address: Datetime-basic Date and time types

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.