Python date arithmetic

1 This article is

Change the defined character type date (eg 2020/7/13) to date type and add date type

2 Code content

test.py


import datetime

##(1)Define the date##

date0='2020/01/06' #Define date 0.
date1='2020/01/09' #Define date 1.


##(2)Convert from character type to date type##

tdate0=datetime.datetime.strptime(date0, '%Y/%m/%d') #Convert date 0 from character type to date type
tdate1=datetime.datetime.strptime(date1, '%Y/%m/%d') #Convert date 1 from character type to date type
print(type(tdate0)) #result:<class 'datetime.datetime'>


##Subtract date 1 and date 2##

dif=tdate1-tdate0 #Calculate the difference between date 1 and date 2.
print(dif.days)   #Result: 3

##Add 2 days to date 1.

tdate2 = tdate1 + datetime.timedelta(days=2)  #Add 2 days to date 1
print(tdate2) #Result: 2020-01-11 00:00:00

Recommended Posts

Python date arithmetic
Python set arithmetic
Python set arithmetic
Date manipulation in Python
[Python2] Date string-> UnixTime-> Date string
[Python3] Get date diff
Get date in Python
Date calculation in python
Get date with python
[Python] Split the date
Date calculation in Python
[python] Convert date to string
Four arithmetic operations in python
Python
String date manipulation in Python
Sort by date in python
How to calculate date with python
Solving Verbal Arithmetic (Ruby / Python Recreation)
kafka python
Conversion of string <-> date (date, datetime) in Python
Python Date / Time Library Reverse Lookup Reference
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore