Python Practical Date-time processing method Summary

Source: Internet
Author: User
Tags timedelta
Principles, DateTime-centric, start-up or transit, into the target object, covering the date conversion processing required in most business scenarios

Steps:

1. Mastering several objects and their relationships
2. Understand the basic operation method of each type of object
3. Transforming through transformation relationships
Objects involved

1. DateTime
Copy the Code code as follows:


>>> Import datetime
>>> now = Datetime.datetime.now ()
>>> now
Datetime.datetime (2015, 1, 12, 23, 9, 12, 946118)
>>> type (now)

2. Timestamp
Copy the Code code as follows:


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


3. Time Tuple
Copy CodeThe 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
Copy the Code code as follows:


>>> Import datetime
>>> Datetime.datetime.now (). Strftime ("%y-%m-%d%h:%m:%s")
' 2015-01-12 23:13:08 '

5. Date
Copy the Code code as follows:


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

DateTime Basic Operation

1. Get the current datetime
Copy the Code code as follows:


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


2. Get Date of day
Copy CodeThe code is as follows:


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


3. Get tomorrow/The first n days

Tomorrow
Copy the Code code as follows:


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


three days ago
Copy CodeThe 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 start and end times of the day (00:00:00 23:59:59)
Copy the Code code 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 the time difference of two datetime
Copy CodeThe code is as follows:


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

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

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

Get last last day of the month (possibly across years)
Copy the Code code 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)

Relationship transformation

A transformation between several relationships

Datetime Object/string/timestamp/time Tuple

Relationship Transformation Examples

DateTime <=> String

DateTime-String
Copy the Code code as follows:


>>> Import datetime
>>> Datetime.datetime.now (). Strftime ("%y-%m-%d%h:%m:%s")
' 2015-01-12 23:13:08 '

DateTime, String-
Copy the Code code 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
Copy the Code code 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
Copy the Code code as follows:


Timetuple = timestamp = datetime [look behind Datetime<=>timestamp]

DateTime <=> Date

DateTime-Date
Copy the Code code as follows:


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

Date, DateTime
Copy the Code code 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
Copy the Code code as follows:


>>> now = Datetime.datetime.now ()
>>> timestamp = Time.mktime (Now.timetuple ())
>>> Timestamp
1421077403.0

Timestamp, datetime
Copy the Code code as follows:


>>> Datetime.datetime.fromtimestamp (1421077403.0)
Datetime.datetime (2015, 1, 12, 23, 43, 23)
  • 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.