How to deal with Date & Time Basics in Python?

How to deal with Date & Time Basics in Python?

How to deal with Date & Time Basics in Python?

This recipe helps you deal with Date & Time Basics in Python


Recipe Objective

Have you tried to change data time or calculate some statistic from date time stamp.

So this is the recipe on how we can deal with Date & Time Basics in Python.

Step 1 - Import the library

from datetime import datetime from datetime import timedelta

We have imported datetime and timedelta which will be needed for the dataset.

Step 2 - Setting up the Data

We have used current datetime for this snippet. now = print(now)

Step 3 - Dealing with Date Time

Here we will be using different functions that we can use on date time.

  • Calculating Current Year
  • print(now.year)
  • Calculating current month
  • print(now.month)
  • Calculating current day
  • print(
  • Calculating current hour
  • print(now.hour)
  • Calculating current minute
  • print(now.minute)
  • Calculating difference between two days
  • delta = datetime(2011, 1, 7) - datetime(2011, 1, 6) print(delta.days)
  • Calculating difference in seconds
  • print(delta.seconds)
  • Creating a datetime
  • start = datetime(2018, 1, 7)
  • Adding twelve days to the time
  • print(start + timedelta(12))
So the output comes as:

2020-10-16 01:50:34.441904






1 day, 0:00:00



2018-01-19 00:00:00

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.