current position:Home>Basic usage of Python datetime Date object: creation, conversion and operation

Basic usage of Python datetime Date object: creation, conversion and operation

2022-02-02 14:13:52 Ice sky

Python datetime

One 、 introduce

Python Of datetime Module is a commonly used date processing module , Module datetime The commonly used object is our date , The two names are exactly the same , When using it, it should be like this :

#  introduce  datetime  modular 
import datetime
#  Use   modular   Methods 
datetime.strptime(x, '%Y')
datetime.today()
...
#  establish  datetime  object 
d = datetime.datetime() #  Two  datetime !!  The first is the module , The second is the class with the same name in the module 
#  Format as mm / DD / yy , This is  datetime  Object method , Not a modular approach 
dStr = d.strftime('%Y-%m-%d')
...

Two 、 establish , modify

python Date object , You can set [year, month, day, hour, minute, second, microsecond] To milliseconds :

import datetime
#  Specify the parameters , For example, which year , Which month , Any time 
#  Must specify  year, month, day
d = datetime.datetime(2021, 10, 10, 0, 0, 0) #  Be careful , Or two  datetime
# 2021, 10, 10, 0, 0

in addition , It can also be done through datetime Some date creation methods of class , Create date object , For example, create an object of the current time :

import datetime
today = datetime.datetime.today()

Return is the current time .

in addition , You can also modify the generated object , Set the time required , Can pass replace Method to modify the time of creation :

d = datetime.datetime(2021, 10, 10, 0, 0, 0)
d.replace(year=2020)
# output
# 2020, 10, 10, 0, 0, 0

Empathy , You can set other time properties second=,month=,day=....

3、 ... and 、 transformation

Sometimes , You need to convert the input time string into a date object for corresponding date calculation , Or date format verification ; When the date data is finally output , You will need to convert the date into a date string in the corresponding format again , At this point, the design to date object (datetime) And date string (str) Mutual conversion between

1. datetime.datetime.strftime(format) Date object format output string

Can pass datetime Object's strftime Method to format and output it as the corresponding string :

dt = datetime.datetime(2021, 1, 1, 0, 0, 0)
datetimeStr = dt.strftime('%Y-%m-%d %H:%i:%S')
print(datetimeStr)
# output 
# 2021-01-01 00:00:00

2. datetime.datetime.strptime(str, format) Convert date string to date object

Can pass datetime Class strptime function , Converts the string into the corresponding date object according to the specified format :

#  Notice that there are still two datetime, Here is  datetime  Class method 
dt = datetime.datetime.strptime('2021-09-01 10:53:13', '%Y-%m-%d') 
#
#  Report errors ! God , Hours, minutes and seconds are not converted 
# 
dt = datetime.datetime.strptime('2021-09-01 10:53:13', '%Y-%m-%d %H:%M:%S')
#  Normal conversion 
# 2021, 9, 1, 10, 53, 13
#

3. datetime.datetime.fromtimestamp(ts) Time stamp date

Convert the timestamp to a string :

datetime.datetime.fromtimestamp(ts)

The output is a date object at the millisecond level

Four 、 operation

The date is calculated by datetime.datetime Date object and timedelta Date increment Add and subtract between to get a new date result , By introducing timedelta To use , Can include up to a week of week Incremental unit of :

td = datetime.timedelta(weeks=0, days=0, hours=0, minutes=0,  seconds=0, milliseconds=0, microseconds=0)

for example :

ts = datetime.timedelta(weeks=1)
# ts
# timedelta(days=7)
ts = datetime.timedelta(minutes=1)
# ts
# timedelta(seconds=60)

namely ,timedelta The basic unit of is :

  • day
  • seoncd

Selecting other units and entering will automatically convert to the nearest unit down , Other units can be calculated from the above two incremental units , for example :

oneYearLater = datetime.datetime.today() + datetime.timedelta(days=365)

Get the date object after one year

copyright notice
author[Ice sky],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202021413495548.html

Random recommended