ABOUT PYTHON -
WORKING WITH DATETIME
In Python, there is also a datetime module that can be used if the program requires the processing of the date and time. For example, how to get the current date and time, or get the date n days after today or n days before today, and so on.
To use date and time processing functions in Python, a first module
datetime is called by issuing the command:
import datetimeor
from datetime import *Now() Function
The now() function is used to read the date and time when the program was run. In this case, the date and time that are read are taken from the time on the computer.
Example:
import datetime
now = datetime.datetime.now()
print('Currently is :', now)Output
Currently is : 2021-12-18 09:30:37.057073Or it can also be written as follows
from datetime import *
now = datetime.now()
print('Currently is :', now)Output
Currently is : 2021-12-18 09:31:43.237285For the note, the time and date that appear are when the program is run.
if you pay attention, the output format of the now() function is year-month-date hours:minutes:seconds. In this case, we cannot change the format as desired. Then what if you want to change the format? To change the format, we can use an exclusive trick. for example, we want to display the current date in date/month/year format.
Example:
from datetime import *now = datetime.now()
datenow = str(now.day)+'/'+str(now.month)+'/'+str(now.year)print('Current date :', datenow)
Output:
Current date : 19/12/2021In addition to the day, month, and year properties, other properties we can use to retrieve the datetime property are hour, minute, and second.
Example:
from datetime import *now = datetime.now()
datenow = str(now.day)+'/'+str(now.month)+'/'+str(now.year)
timenow = str(now.hour)+':'+str(now.minute)+':'+str(now.second)print('Current date :', datenow)
print('Current time :', timenow)
Output:
Current date : 19/12/2021
Current time : 10:56:32In addition to using now(), we can also use today() to get the current date and time.
Get Danang Kurniawan’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
from datetime import *
now = datetime.today()
print('Currently is :', now)Output:
Currently is : 2021-12-19 10:59:54.215003Date() Function
The date() function is used to retrieve the date part (YYYY-MM-DD) from a series date and time. For example:
from datetime import *
now = datetime.date(datetime.now())
print("Today's date :", now)Output:
Today's date : 2021-12-19Time() Function
Next, how to extract the time part (hours:minutes:seconds) from date format resulting datetime, let’s say from now() result? The trick is to use the time() function.
Example:
from datetime import *
now = datetime.time(datetime.now())
print("Current time :", now)Output:
Current time : 11:08:54.573890Timedelta() Function
The next problem, for example, today is February 10, 2018. How do we get the next 45 days or the previous 10 days using python? No need to worry, because in Python there is a timedelta() feature that can make it easier for us to determine the time ahead or before.
Consider the following example program to determine what date after 15 days from today’s date.
from datetime import *x = datetime.date(datetime.now())
print("Current date :", x)y = x + timedelta(days=15)
print('15 more days is the date :', y)
Output:
Current date : 2021-12-19
15 more days is the date : 2022-01-03Then what if you want to determine 20 days before today? The method is quite easy, namely by providing the (-) operator.
Example:
from datetime import *x = datetime.date(datetime.now())
print("Current date :", x)y = x + timedelta(days=-20)
print('20 previous days is the date :', y)
Output:
Current date : 2021-12-19
20 previous days is the date : 2021-11-29In addition to the day’s parameter in timedelta(), other parameters that can be used are presented in Table 11.1
Case Study 11.1
Create a function diffDate(x) which is used to calculate the difference between today and date x (in the string format ‘YYYY-MM-DD’). The function returns an integer which is the difference between date x and today’s date. example:
Suppose today’s date is ‘2018–12–26’, then if the function is called with if Date(‘2018–12–30’), it will return the value 4.
Answer:
from datetime import *
tgl = ‘2021–12–24’
def diffDate(x):
target = datetime.strptime(x, ‘%Y-%m-%d’)
now= datetime.strptime(str(datetime.date(datetime.now())), ‘%Y-%m-%d’)
difference = target — now
return difference.daysdiffDate(tgl)
The output of the above program is not displayed in the IDLE shell but contains the difference in days from the current date.






