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
today = np.datetime64( 'today' , 'D' )
print ( "Today: " , today)
yesterday = np.datetime64( 'today' , 'D' )
- np.timedelta64( 1 , 'D' )
print ( "Yestraday: " , yesterday)
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
today = np.datetime64( 'today' , 'D' )
print ( "Today: " , today)
before_2_day = np.datetime64( 'today' , 'D' )
- np.timedelta64( 2 , 'D' )
print ( "before_2_day : " , before_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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!