Python print current date,time,hour,minute,increment each

Python program to print current date, time, hour, minute, and increment each :

In this tutorial, we will learn how to print the current date , time and hour using python 3 . Also, how to increment the day, hour or minute to the current date. Let’s take a look :

datetime module :

We are going to use datetime module of python in this example. For that, use import datetime on the top of the program to import. To get the current local date, we are using method date.today(). Again, to add one date or hour to the current date, use timedelta() method. For example, to increment the current date-time by one hour , use timedelta(hours = 1). You can use days,seconds,microseconds,milliseconds,minutes,hours and weeks with timedelta.

Python example to use datetime and timedelta :

import datetime

def currentTime():
	print("Current date and time : ")
	today = datetime.datetime.strftime(datetime.datetime.today() , '%d/%m/%Y-%Hh/%Mm')
	print(today)

oneDayLater = datetime.datetime.today() + datetime.timedelta(days = 1)
currentTime()
print("Time after one day : ")
print(datetime.datetime.strftime(oneDayLater , '%d/%m/%Y-%Hh/%Mm'))


fourWeeksLater = datetime.datetime.today() + datetime.timedelta(weeks = 4)
print("")
currentTime()
print("Time after four weeks : ")
print(datetime.datetime.strftime(fourWeeksLater , '%d/%m/%Y-%Hh/%Mm'))


oneHourLater = datetime.datetime.today() + datetime.timedelta(hours = 1)
print("")
currentTime()
print("Time after one hour : ")
print(datetime.datetime.strftime(oneHourLater , '%d/%m/%Y-%Hh/%Mm'))


minutesLater = datetime.datetime.today() + datetime.timedelta(minutes = 15)
print("")
currentTime()
print("Time after 15 minutes : ")
print(datetime.datetime.strftime(minutesLater , '%d/%m/%Y-%Hh/%Mm'))

Output :

Current date and time :
11/02/2018-20h/56m
Time after one day :
12/02/2018-20h/56m

Current date and time :
11/02/2018-20h/56m
Time after four weeks :
11/03/2018-20h/56m

Current date and time :
11/02/2018-20h/56m
Time after one hour :
11/02/2018-21h/56m

Current date and time :
11/02/2018-20h/56m
Time after 15 minutes :
11/02/2018-21h/11m

Leave a Reply