A simple summary of the time and date processing methods in Python, and a python Processing Method

Source: Internet
Author: User
Tags timedelta

A simple summary of the time and date processing methods in Python, and a python Processing Method
This article mainly introduces the summary of Python practical date and time processing methods, this article explains how to get the current datetime, get the date of the current day, get tomorrow/N days before, get the start and end time of the current day (00:00:00) get the time difference between two datetime, get this week/this month/last month and other practical methods, need friends can refer to the original address: http://www.cnblogs.com/wenBlog/p/6097220.html

The conversion principle is datetime-centered, starting point or intermediate. It is converted to the target object and covers 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)
<Type 'datetime. datetime'>

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

Copy the Code 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)

Address: http://www.cnblogs.com/wenBlog/p/6097220.html

 

Additional

    1. # Datetime. date (datetime. date. today (). year, datetime. date. today (). month, 1)
    2. # Datetime. date. today (). replace (day = 1) on the first day of the current month)
    3. # 1st day of last month (datetime. date. today (). replace (day = 1)-datetime. timedelta (1). replace (day = 1)

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.