In NumPy with the help of NumPy.datetime64(‘today’, ‘D’), we will find today date and if we want some date before today then we will subtract the no-of-date with the help of np.timedelta64() from today. And if we want some date after today then we will add the no of date with the help of np.timedelta64() from today.
For yesterday, we will subtract 1 date, for tomorrow we will add 1 date.
Example #1: Get the dates yesterday, today, and tomorrow.
Python3
import numpy as np # for today today = np.datetime64( 'today' , 'D' ) print ( "Today: " , today) # for yesterday yesterday = np.datetime64( 'today' , 'D' ) - np.timedelta64( 1 , 'D' ) print ( "Yestraday: " , yesterday) # for tomorrow tomorrow = np.datetime64( 'today' , 'D' ) + np.timedelta64( 1 , 'D' ) print ( "Tomorrow: " , tomorrow) |
Output:
Today: 2020-08-15 Yestraday: 2020-08-14 Tomorrow: 2020-08-16
Example #2: Get the dates in the interval.
Python3
import numpy as np # for today today = np.datetime64( 'today' , 'D' ) print ( "Today: " , today) # for before_2_day before_2_day = np.datetime64( 'today' , 'D' ) - np.timedelta64( 2 , 'D' ) print ( "before_2_day : " , before_2_day) # for after_2_day after_2_day = np.datetime64( 'today' , 'D' ) + np.timedelta64( 2 , 'D' ) print ( "after_2_day :" , after_2_day) |
Output:
Today: 2020-08-15 before_2_day : 2020-08-13 after_2_day : 2020-08-17