Python practical date and time processing method summary-Python tutorial

Source: Internet
Author: User
Tags timedelta
This article mainly introduces a summary of the Python practical date and time processing methods. This article describes how to get the current datetime, get the date of the day, get the N days before tomorrow, get the start and end time of the day (00: 00: 0023: 59: 59), obtain the time difference between two datetime values, and obtain the last day of the last month of this week. if you need a datetime Value, refer to the following principle, conversion from the start point or intermediate point to the target object, covering the date conversion processing required in most business scenarios

Steps:

1. Master several objects and their relationships
2. understand the basic operation methods of each type of object
3. conversion through conversion relationships
Objects involved

1. datetime

The code is as follows:


>>> Import datetime
>>> Now = datetime. datetime. now ()
>>> Now
Datetime. datetime (2015, 1, 12, 23, 9, 12,946)
>>> Type (now)

2. timestamp

The code is as follows:


>>> Import time
>>> Time. time ()
1421075455.568243


3. time tuple

The code is as follows:


>>> Import time
>>> Time. localtime ()
Time. struct_time (maid = 2015, tm_mon = 1, tm_mday = 12, tm_hour = 23, tm_min = 10, tm_sec = 30, tm_wday = 0, tm_yday = 12, tm_isdst = 0)

4. string

The code is as follows:


>>> Import datetime
>>> Datetime. datetime. now (). strftime ("% Y-% m-% d % H: % M: % S ")
'2017-01-12 23:13:08'

5. date

The code is as follows:


>>> Import datetime
>>> Datetime. datetime. now (). date ()
Datetime. date (2015, 1, 12)

Datetime Basic operations

1. get the current datetime

The code is as follows:


>>> Import datetime
>>> Datetime. datetime. now ()
Datetime. datetime (2015, 1, 12, 23, 26, 24,475 680)


2. get the date of the current day

The code is as follows:


>>> Datetime. date. today ()
Datetime. date (2015, 1, 12)


3. get tomorrow/N days before

Tomorrow

The code is as follows:


>>> Datetime. date. today () + datetime. timedelta (days = 1)
Datetime. date (2015, 1, 13)


Three days ago

The code is as follows:


>>> Datetime. datetime. now ()
Datetime. datetime (2015, 1, 12, 23, 38, 55,492 226)
>>> Datetime. datetime. now ()-datetime. timedelta (days = 3)
Datetime. datetime (2015, 1, 9, 23, 38, 57,593 63)

4. obtain the start time and end time of the current day (00:00:00)

The code is as follows:


>>> Datetime. datetime. combine (datetime. date. today (), datetime. time. min)
Datetime. datetime (2015, 1, 12, 0, 0)
>>> Datetime. datetime. combine (datetime. date. today (), datetime. time. max)
Datetime. datetime (2015, 1, 12, 23, 59, 59,999 999)


5. obtain the time difference between two datetime values.

The code is as follows:


>>> (Datetime. datetime (,)-datetime. datetime. now (). total_seconds ()
44747.768075

6. obtain the last day of the week/month

This week

The code is as follows:


>>> Today = datetime. date. today ()
>>> Today
Datetime. date (2015, 1, 12)
>>> Sunday = today + datetime. timedelta (6-today. weekday ())
>>> Sunday
Datetime. date (2015, 1, 18)

This month

The code is as follows:


>>> Import calendar
>>> Today = datetime. date. today ()
>>>_, Last_day_num = calendar. monthrange (today. year, today. month)
>>> Last_day = datetime. date (today. year, today. month, last_day_num)
>>> Last_day
Datetime. date (2015, 1, 31)

Obtain the last day of the previous month (possibly across years)

The code is as follows:


>>> Import datetime
>>> Today = datetime. date. today ()
>>> First = datetime. date (day = 1, month = today. month, year = today. year)
>>> LastMonth = first-datetime. timedelta (days = 1)

Link conversion

Conversion between several relationships

Datetime Object/String/timestamp/time tuple

Link conversion example

Datetime <=> string

Datetime-> string

The code is as follows:


>>> Import datetime
>>> Datetime. datetime. now (). strftime ("% Y-% m-% d % H: % M: % S ")
'2017-01-12 23:13:08'

String-> datetime

The code is as follows:


>>> Import datetime
>>> Datetime. datetime. strptime ("2014-12-31 18:20:10", "% Y-% m-% d % H: % M: % S ")
Datetime. datetime (2014, 12, 31, 18, 20, 10)

Datetime <=> timetuple

Datetime-> timetuple

The code is as follows:


>>> Import datetime
>>> Datetime. datetime. now (). timetuple ()
Time. struct_time (maid = 2015, tm_mon = 1, tm_mday = 12, tm_hour = 23, tm_min = 17, tm_sec = 59, tm_wday = 0, tm_yday = 12, tm_isdst =-1)

Timetuple-> datetime

The code is as follows:


Timetuple => timestamp => datetime [see the following datetime <=> timestamp]

Datetime <=> date

Datetime-> date

The code is as follows:


>>> Import datetime
>>> Datetime. datetime. now (). date ()
Datetime. date (2015, 1, 12)

Date-> datetime

The code is as follows:


>>> Datetime. date. today ()
Datetime. date (2015, 1, 12)
>>> Today = datetime. date. today ()
>>> Datetime. datetime. combine (today, datetime. time ())
Datetime. datetime (2015, 1, 12, 0, 0)
>>> Datetime. datetime. combine (today, datetime. time. min)
Datetime. datetime (2015, 1, 12, 0, 0)

Datetime <=> timestamp

Datetime-> timestamp

The code is as follows:


>>> Now = datetime. datetime. now ()
>>> Timestamp = time. mktime (now. timetuple ())
>>> Timestamp
1421077403.0

Timestamp-> datetime

The code is as follows:


>>> Datetime. datetime. fromtimestamp (1421077403.0)
Datetime. datetime (2015, 1, 12, 23, 43, 23)

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.