How to calculate date with python

Hello sekitaka.

Using the timedelta object of the datetime module of python, you can easily add or subtract the date and time and calculate the difference between the two datetime.

Example of use

# coding:utf-8
# !/usr/bin/python
from datetime import datetime,timedelta
import pytz

#Addition and subtraction based on the current time
timezone = pytz.timezone('Asia/Tokyo')
now = datetime.now(tz=timezone)
delta = timedelta(days=+1)
tomorrow = now + delta
print "now{0}".format(now)
print "One day later{0}".format(tomorrow)

#Calculate the difference between two dates
delta = tomorrow - now
print "The difference is{0}Day".format(delta.days)

For more detailed usage, please refer to Official Document.

Recommended Posts

How to calculate date with python
Python: How to use async with
How to get started with Python
How to use FTP with Python
How to do portmanteau test with python
How to display python Japanese with lolipop
How to enter Japanese with Python curses
[Python] How to calculate MAE and RMSE
[Python] How to deal with module errors
How to install python3 with docker centos
How to install Python
How to install python
Get date with python
How to calculate "xx time" in one shot with Python timedelta
How to upload with Heroku, Flask, Python, Git (4)
How to read a CSV file with Python 2/3
How to enjoy programming with Minecraft (Ruby, Python)
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
Strategy on how to monetize with Python Java
[Python] How to change the date format (display format)
[Python] How to draw multiple graphs with Matplotlib
[Python] How to read excel file with pandas
How to crop an image with Python + OpenCV
Convert Windows epoch values to date with python
How to specify attributes with Mock of python
How to measure execution time with Python Part 1
How to use tkinter with python in pyenv
[Python] How to handle Japanese characters with openCV
[Python] How to compare datetime with timezone added
How to measure execution time with Python Part 2
How to get the date and time difference in seconds with python
Connect to BigQuery with Python
[2020.8 latest] How to install Python
[python] Convert date to string
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
Connect to Wikipedia with Python
How to update Python Tkinter to 8.6
Post to slack with Python 3
How to update with SQLAlchemy?
How to cast with Theano
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to Delete with SQLAlchemy?
How to use Python bytes
How to convert / restore a string with [] in python
How to add help to HDA (with Python script bonus)
[Python] How to draw a line graph with Matplotlib