Datetime, c # datetime

Source: Internet
Author: User
Tags timedelta

Datetime, c # datetime

# Import datetime

# String to datetime:

# Notice:

# When the String type is converted to the datetime type, the date format must match

  a = '2016-03-23'  b = datetime.datetime.strptime(a,'%Y-%m-%d')

# Datetime to String:

# Notice:

# When datetime is converted to the String type, it can be converted to any format

  now = datetime.datetime.now()

# Obtain the current time:

  now = datetime.datetime.now()

# Get today's date:

    today = datetime.date.today()

# Obtain the date of yesterday:

    yesterday = today - datetime.timedelta(days=1)

# Obtain n days, n hours, before and after a date:

# Example: Get the date of the previous 8 days

  now = datetime.datetime.now()  date_before_8 = now - datetime.timedelta(days=8)

# Example: Get the date 12 days after the present:

  date_after_12 = now + datetime.timedelta(days=12)

# Example: 8 hours before the current time:

  date_before_8_hours = now - datetime.timedelta(hours=8)

# Obtain the date and time objects of datetime respectively:

  now = datetime.datetime.now().  now.date()  now.time()

# Obtain the year, month, and day of datetime respectively:

  now.year  now.month  now.day

# Obtain the time difference between two datetime values, and return the number of days:

  (date1 - date2).days

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.