How to get the dates of yesterday today and tomorrow using numpy

This recipe helps you get the dates of yesterday today and tomorrow using numpy

Recipe Objective

Working with datetime, we might be interested to find the dates of today, yesterday or event after 365 days from today.

So this recipe is a short example on how to get the dates of yesterday, today and tomorrow. Let's get started.

Access Product Recommendation System Project with Source Code

Step 1 - Import the library

import numpy as np

Let's pause and look at these imports. Numpy is generally helpful in data manipulation while working with arrays. It also helps in performing mathematical operation.

Step 2 - Generating dates

today = np.datetime64('today', 'D') yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D') tomorrow =np.datetime64('today', 'D') + np.timedelta64(1, 'D')

Here, we have used datetime64 function and timedelta64 function of numpy library for computing dates.

Step 3 - Printing dates

print(today) print(yesterday) print(tomorrow)

Simply use print function to print the dates of yesterday, today and tomorrow.

Step 4 - Let's look at our dataset now

Once we run the above code snippet, we will see:

2020-11-04
2020-11-03
2020-11-05

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker