Python time acquisition and transformation of knowledge summary

  • 2020-05-24 05:43:53
  • OfStack

Time processing is the most common requirement in our daily development, such as: get the current datetime, get the date of the day, get the tomorrow/N days before, get the start and end of the day (00:00:23:59:59), get the time difference between the two datetime, get the last day of the week/month/month, etc. While these transformations seem messy and hard to remember, today we will sum up the time processing of Python.

Principle: with datetime as the center, the starting point or the intermediate, is converted to the target object, which 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 class of objects

3. Transform through transforming relationships

datetime is a combination of date and time, including all information about date and time.

The function prototype is:

datetime. datetime (year, month, day[ , hour[ , minute[ , second[ , microsecond[ , tzinfo] ] ] ] ] )
The meaning of each parameter is the same as that in the constructor of date and time. Note the range of parameter values.

Example:

1. Get the datetime object

The code is as follows:


import datetime
now = datetime.datetime.now()
# Output: datetime.datetime(2016, 11, 26, 8, 34, 30, 876359)

2. Get timestamp (timestamp)

The timestamp is the number of seconds since January 1, 1970 (00:00:00 GMT). It is also known as Unix timestamp (Unix Timestamp).

The code is as follows:


import time
time.time()
# Output: 1480120686.733905

3. Get time tuple (tuple)

The code is as follows:


import time
time.localtime()
# Output: time.struct_time(tm_year=2016, tm_mon=11, tm_mday=26, tm_hour=8, tm_min=39, tm_sec=33, tm_wday=5, tm_yday=331, tm_isdst=0)

4. Get time string(string)

String format parameter list:
datetime. strftime (format)
%a: short for week. For example, Wednesday is Web
%A: write it all for the week. If Wednesday is Wednesday
%b: short for month. For example, it was Apr in April
%B: full month. For example, April in April
%c: a string representation of the date and time. (e.g. 04/07/10 10:43:39)
%d: the number of days in a month
%f: microseconds (range [0,999999])
%H: hours (24-hour system, [0, 23])
%I: hours (12-hour system, [0, 11])
%j: the number of days in a year [001,366] (the day of the year)
%m: month ([01,12])
%M: minutes ([00,59])
%p: AM or PM
%S: seconds (range [00,61], why not [00, 59], refer to python manual ~_~)
%U: week is the week of the year (Sunday is the first day of the week
%w: today is the number of days in the week, in the range of [0, 6], 6 being Sunday
%W: week is the number of weeks of the year (the week of the year), and Monday is the first day of the week
%x: date string (e.g. 04/07/10)
%X: time string (e.g. : 10:43:39)
%y: year with two Numbers
%Y: year with four Numbers
%z: time interval from utc (return empty string if local time)
%Z: time zone name (return empty string if local time)
%%: %% = > %

The code is as follows:


import datetime
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2016-11-26 08:40:39'

5.date (date)

The code is as follows:


import datetime
datetime.datetime.now().date()
datetime.date(2016, 11, 26)

6. Get day date

The code is as follows:


import datetime
datetime.date.today()
datetime.date(2016, 11, 26)

7. Get tomorrow/N days before

Tomorrow,

The code is as follows:


import datetime
datetime.date.today() + datetime.timedelta(days=1)
datetime.date(2016, 11, 27)

Two days ago

The code is as follows:


import datetime
>>> datetime.datetime.now()
datetime.datetime(2016, 11, 26, 8, 42, 59, 665368)
>>> datetime.datetime.now() - datetime.timedelta(days=3)
datetime.datetime(2016, 11, 24, 8, 43, 14, 696948)

8. Get the start and end of the day (00:00:00 23:59:59)

The code is as follows:


import datetime
datetime.datetime.combine(datetime.date.today(), datetime.time.min)
datetime.datetime(2016, 11, 26, 0, 0)
datetime.datetime.combine(datetime.date.today(), datetime.time.max)
datetime.datetime(2016, 11, 26, 23, 59, 59, 999999)

9. Get the time difference between the two datetime

The code is as follows:


import datetime
(datetime.datetime(2016,12,13,12,0,0) - datetime.datetime.now()).total_seconds() 
1480506.809658

10. Get the last day of the week/month/month

This week,

The code is as follows:


import time
time.time()
# Output: 1480120686.733905
0

This month,

The code is as follows:


import time
time.time()
# Output: 1480120686.733905
1

11. Get the last day of last month (possibly New Year's eve)

The code is as follows:


import time
time.time()
# Output: 1480120686.733905
2

Other examples of use:

The code is as follows:


# During the month 1 No. 
datetime.date(datetime.date.today().year,datetime.date.today().month,1)
# During the month 1 No.  
datetime.date.today().replace(day=1) 
# Last month, 1 No.  
(datetime.date.today().replace(day=1) - datetime.timedelta(1)).replace(day=1)

Related articles: