Daily use of the Python datetime module

Source: Internet
Author: User

Daily use of the Python datetime module


[10:52:43] [Email protected]| ~]# python

Python 2.7.12 (Default, June 27 2017, 11:19:01)

[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2

Type "Help", "copyright", "credits" or "license" for more information.

>>> Import datetime

# Get current time, output in string format

>>> datetime.datetime.now () # Output current time, DateTime format

Datetime.datetime (2017, 7, 31, 10, 53, 8, 430324)

>>> Print type (Datetime.datetime.now ())

<type ' Datetime.datetime ' >

>>> Datetime.datetime.now (). Strftime ("%F%T") # DateTime format converted to str format

' 2017-07-31 10:53:46 '

>>> Print type (Datetime.datetime.now (). Strftime ("%F%T"))

<type ' str ' >

# Calculate Time Difference

>>> start = Datetime.datetime.now ()

>>> end = Datetime.datetime.now ()

>>> print Start, type (start)

2017-07-31 10:58:36.875319 <type ' datetime.datetime ' >

>>> print end, type (end)

2017-07-31 10:58:51.778787 <type ' datetime.datetime ' >

>>> time_seconds = (end-start). Seconds # How many seconds difference

>>> print Time_seconds, type (time_seconds)

<type ' int ' >

>>> time_microseconds = (end-start). Microseconds # How many microseconds difference

>>> print Time_microseconds, type (time_microseconds)

903468 <type ' int ' >

>>> time_days = (end-start). Days # How long is the difference?

>>> print time_days, type (time_days)

0 <type ' int ' >

>>> delta_time = float (str (time_days*86400 + time_seconds) + "." + str (time_microseconds)) # Calculate the difference in number of seconds (three parts need to be added)

>>> print Delta_time, type (delta_time)

14.903468 <type ' float ' >

# Convert the time of the STR format to the datetime format

>>> time_string = "2012-02-02 22:22:22"

>>> print type (time_string)

<type ' str ' >

>>> time_datetime = Datetime.datetime.strptime (time_string, "%y-%m-%d%h:%m:%s")

>>> print Time_datetime, type (time_datetime)

2012-02-02 22:22:22 <type ' datetime.datetime ' >

# Calculates a time difference of two times

>>> T1 = "2012-02-02 22:22:22"

>>> t2 = "2012-02-28 11:11:11"

>>> t1_datetime = datetime.datetime.strptime (T1, "%y-%m-%d%h:%m:%s")

>>> t2_datetime = datetime.datetime.strptime (T2, "%y-%m-%d%h:%m:%s")

>>> T2_datetime

Datetime.datetime (2012, 2, 28, 11, 11, 11)

>>> T1_datetime

Datetime.datetime (2012, 2, 2, 22, 22, 22)

>>> print (t2_datetime-t1_datetime). Days

25

>>> print (t2_datetime-t1_datetime). seconds

46129

>>> delta_t = delta_days*86400 + delta_seconds # Calculate the difference in number of seconds

>>> print delta_t, type (delta_t)

2206129 <type ' int ' >

Attention:

T2 time part converted to seconds: 40271

T1 time part converted to seconds: 80542

So the number of seconds to subtract, you need to automatically borrow a day from the days, that is 86400+40271-80542=46219

And the days subtract into 28-1-2=25


Daily use of the Python datetime module

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.