Open In App
Related Articles

Get the dates of Yesterday, Today, and Tomorrow using NumPy

Improve Article
Improve
Save Article
Save
Like Article
Like

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

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!

Last Updated : 26 Nov, 2020
Like Article
Save Article
Similar Reads
Related Tutorials