Search Here

Python DateTime, Strftime, Strptime and string to DateTime conversion

Python DateTime Module

This Module provides sufficient features related to date and time. Its a quite useful module in python.

Basic Examples

import datetime

print(datetime.datetime.today()) # This prints today's date with time

print(datetime.datetime.now()) # This prints present date with time with more precise than datetime.datetime.today()

print(datetime.datetime.min) #This prints initial date with time

print(datetime.datetime.max) #This prints max date with time

print(datetime.datetime.today().year) #This prints year of date object

print(datetime.datetime.today().month) #This prints month of date object

print(datetime.datetime.today().day) #This prints day of date object

print(datetime.date(2010,12,15)) 
#-->> 2010-12-15

#PYTHON OUTPUT
2019-01-22 17:18:21.711971
2019-01-22 17:18:21.712027
0001-01-01 00:00:00
9999-12-31 23:59:59.999999
2019
1
22
2010-12-15    

dateTime.date() syntax

datetime.date(YEAR, MONTH, DAY)

For string representation of Date-Time use strftime() method this returns date object as string.

import datetime

now = datetime.datetime.now()

print(now.strftime("%d")) # This prints Day 
print(now.strftime("%B")) # This prints Month's fullname 
print(now.strftime("%Y")) # This prints full year 

print(format(now.strftime("%d-%B-%Y")))
print("Today's date is: {}".format(now.strftime("%d-%B-%Y")))

#PYTHON OUTPUT
22
January
2019
22-January-2019
Today's date is: 22-January-2019

Convert String to time

import datetime

date="2018-12-11"
formatted_date=datetime.datetime.strptime(date, "%Y-%m-%d")

print(formatted_date)

#PYTHON OUTPUT
2018-12-11 00:00:00

Difference between dates

Finding a difference in dates using date object

import datetime

f_date = datetime.date(2008, 8, 18)
t_date = datetime.date(2010, 8, 18)
diff_date = t_date-f_date

print(diff_date)

#PYTHON OUTPUT
730 days, 0:00:00

The above method gives use difference in terms of days. but what if we want the difference in hours, minutes, months or years.
Then we need to use python DateTime modules strptime() method. This will convert the string to a python DateTime object.

from datetime import datetime

format = '%Y-%m-%d %H:%M:%S.%f'
date1 = '2008-02-01 01:01:01.001'
date2 = '2009-02-01 02:02:02.002'

f_date = datetime.strptime(date2, format)

t_date = datetime.strptime(date1, format)

date_diff = f_date-t_date
print("Date Difference: {}".format(date_diff))
print("Difference in Days: {} ".format(date_diff.days))
print("Difference in Microseconds: {} ".format(date_diff.microseconds))
print("Difference in Seconds: {} ".format(date_diff.seconds))

#PYTHON OUTPUT
Date Difference: 366 days, 1:01:01.001000
Difference in Days: 366 
Difference in Microseconds: 1000 
Difference in Seconds: 3661 

Working with Python time module

This module is helpful in working with different time-related methods, timezone conversion and time manipulation.

Below describe some of the useful methods in time Module

Getting current time using the time module.

from time import gmtime, strftime

print(gmtime())

#PYTHON OUTPUT
time.struct_time(tm_year=2019, tm_mon=1, tm_mday=25, tm_hour=9, tm_min=39, tm_sec=56, tm_wday=4, tm_yday=25, tm_isdst=0)

gmtime() the method will print time in tuple form to convert this to readable string use strftime() method.

from time import gmtime, strftime

print(gmtime())
print(strftime("%I:%M:%S %p",gmtime()))

#PYTHON OUTPUT
09:39:56 AM

Note: gmtime() method may not give precise time insisted we can use localtime() method for time accuracy.

from time import gmtime, strftime, localtime
print(strftime("%I:%M:%S %p",localtime()))

#PYTHON OUTPUT
03:14:00 PM

Creating a time object takes two arguments in strptime() method. This method converts time string to time object.

import time

new_time=time.strptime("12 december 2016 09:24:58 AM", "%d %B %Y %H:%M:%S %p")

print(new_time)
print(time.strftime("%I:%M:%S %p",new_time))

#PYTHON OUTPUT
time.struct_time(tm_year=2016, tm_mon=12, tm_mday=12, tm_hour=9, tm_min=24, tm_sec=58, tm_wday=0, tm_yday=347, tm_isdst=-1)
09:24:58 AM

For additional information of using DateTime, you can refer our article on DateTime module with programs and also link to python documentation provided.

Related Posts