Python datetime,timedelta,strftime functions with examples

Spread the love

In this article, we will discuss about python datetime functions with examples. You can also how to manipulate date and time. In these examples i am using python 3.6 version.

Python has a inbuild module for datetime. First we need to import that function to work with date and time

In this program we wrote program to get the current date and time

import datetime datetime = datetime.datetime.now() print("current date and time",datetime) date= datetime.date() print("current date ",date) time= datetime.time() print("current time",time)

In datatime has 4 submodules date, time, datetime, timedelta. In this article, we will discuss one by one.

1. Date module in python:

It has date object with year, month,date.

#program to work with date in python

import datetime date = datetime.date(2019, 3, 1) print(date)

# Program to get month, year, day

you need to call date object with year  to call year or month to call month or day to call date.

import datetime date = datetime.date(2019, 3, 1) print("printing date",date ) print("printing year",date.year) print("printing month",date.month) print("printing day",date.day)

2. Time module in python:

By using time module we can work with hours, minutes, seconds

#program to print time

import datetime from datetime import time time= time() print("printing time",time)

#program to print given time with specifying hours, minites, seconds and micro seconds

import datetime from datetime import time t= time(4,30,6,55) print("printing input time",t) print("printing hours : ",t.hour) print("printing minutes: ",t.minute) print("printing seconds: ",t.second) print("printing microsecond:", t.microsecond)

3. Datetime module in python:

It has a date class to work with date and time requirements.

If you want to work with date and time, you need to import datetime from the python library

import datetime from datetime import datetime dt= datetime(2019,3,1) print(dt) dt= datetime(2019,3,1,12,11,4,3) print(dt)

#program to get year, month, date, or time from the datetime object

import datetime from datetime import datetime dt= datetime(2019,3,1) print(dt) dt= datetime(2019,3,1,12,11,4,3) print("year",dt.year) print("month",dt.month) print("day",dt.day) print("hours",dt.hour) print("minutes",dt.minute) print("seconds",dt.second) print("micro seconds",dt.microsecond)

4. Timedelta module in python:

If you want to know the difference between times, timedelta module will be useful. It will returns the differences between to dates and time.

import datetime from datetime import datetime dt1= timedelta(2019,3,1) dt2= timedelta(2018,6,5) dt3 = dt1-dt2 print("type of dt2=", type(dt2)) print("type of dt3 =", type(dt3))

Python strftime Convert datetime to format:

If you are writing a program, sometimes you need to display day with thurday or some other formats. at that time this function is useful for formatting datetime.

strftime

Python strptime() function String to Datetime format: 

If you have any requirement from converting string to dateandtime format this function will be useful.

 

strftime
admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *