Python Practical Date-time processing method

Source: Internet
Author: User
Tags datetime min timedelta

This article mainly introduces the Python practical Date Time processing method Summary, this article explained obtains the current datetime, obtains the day date, obtains tomorrow/the first n days, obtains the day start and the end time (00:00:00 23:59:59), obtains two datetime , get this week/month/month last day, etc. practical methods, need friends can refer to the following

Principle, DateTime centric, starting point or relay, translated into target object, covering most business scenarios required date conversion processing

Steps:

1. Mastering several objects and their relationships

2. Understand the basic operating methods of each type of object

3. Transforming Relations through transformation

involved objects

1. DateTime

The code is as follows:

>>> Import datetime

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

>>> now

Datetime.datetime (2015, 1, 12, 23, 9, 12, 946118)

>>> 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 (tm_year=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")

' 2015-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 current DateTime

The code is as follows:

>>> Import datetime

>>> Datetime.datetime.now ()

Datetime.datetime (2015, 1, 12, 23, 26, 24, 475680)

2. Get date of the day

The code is as follows:

>>> Datetime.date.today ()

Datetime.date (2015, 1, 12)

3. Get tomorrow/First n days

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, 492226)

>>> Datetime.datetime.now ()-Datetime.timedelta (days=3)

Datetime.datetime (2015, 1, 9, 23, 38, 57, 59363)

4. Get the day start and end time (00:00:00 23:59:59)

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, 999999)

5. Get a time difference of two datetime

The code is as follows:

>>> (Datetime.datetime (2015,1,13,12,0,0)-Datetime.datetime.now ()). Total_seconds ()

44747.768075

6. Get this week/month/last day of last month

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)

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)

Get last day of last month (May span years)

The code is as follows:

>>> Import datetime

>>> today = Datetime.date.today ()

>>> i = datetime.date (Day=1, Month=today.month, Year=today.year)

>>> lastmonth = First-datetime.timedelta (Days=1)

Relationship Conversion

The transformation between several relationships

Datetime Object/string/timestamp/time Tuple

Relational conversion Examples

DateTime <=> String

DateTime-> String

The code is as follows:

>>> Import datetime

>>> Datetime.datetime.now () strftime ("%y-%m-%d%h:%m:%s")

' 2015-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 (tm_year=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 Back 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)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.