Sometimes, it might be required to log in the current date into a database or to keep a tab on certain events while you are working on some application in Python. This can be accomplished in a variety of ways, using different modules and classes in Python.
We will look at a couple of methods in which the current date and time can be obtained in Python.
1. Using today
method from date module
The date
class present in the datetime module contains a method named today
. This can be used to fetch current date.
Time for an example:
from datetime import date
today = date.today()
print("Type of object", type(today))
print("Today's date is:", today)
Output:
Type of object <class 'datetime.date'>
Today's date is: 2019-10-28
2. Getting the current date in a different format
The current date can be obtained in different formats, by programming it to perform so, using the strftime
method which is present in the datetime module. It helps to obtain the string representation of the date.
Time for an example:
from datetime import date
today = date.today()
date_1 = today.strftime("%d/%m/%Y") # format is dd/mm/yy
print("Format 1- dd/mm/yy =", date_1)
date_2 = today.strftime("%B %d, %Y") # format is of the type text
print("Format 2- text =", date_2)
date_3 = today.strftime("%m/%d/%y") # format is mm/dd/y
print("Format is mm/dd/y =", date_3)
date_4 = today.strftime("%b-%d-%Y") # abbreviation for day and year
print("Format is text for day and year =", date_4)
Output:
Format 1- dd/mm/yy = 28/10/2019
Format 2- text = October 28, 2019
Format is mm/dd/y = 10/28/19
Format is text for day and year = Oct-28-2019
Note: In real applications, the module is imported and the method of the class is called in a single line. This is quick way of getting it done or the pythonian way, and has been shown below:
import datetime
print("Using now")
print(datetime.datetime.now().strftime("%Y/%m/%d"))
print("\n")
print("Using today")
print(datetime.date.today())
Output:
Using now
2019/10/28
Using today
2019-10-28
Following are some different options which can be used with the strftime
mthod:
-
%a
- Current location's weekday abbreviation
-
%A
- Current location's full weekday name
-
%b
- Current location's abbreviated month
-
%B
- Current location's full month name
-
%c
- Current location's date and time representation
-
%d
- The day of the month as a decimal number [01,31]
-
%H
- The hour (24-hour clock) as a decimal number [00,23]
-
%I
- The hour (12-hour clock) as a decimal number [01,12]
-
%j
- The day of the year as a decimal number [001,366]
-
%m
- The month as a decimal number [01,12]
-
%M
- The minute as a decimal number [00,59]
-
%p
- Current location's AM or PM
-
%S
- The second as a decimal number [00,61]
-
%U
- The week number of a year (Begins with Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year before the first Sunday is considered as week 0
-
%w
- The weekday as a decimal number [0(Sunday),6]
-
%W
- The week number of a year (Begins with Monday as the first day of the week) as a decimal number [00,53]. All days in a new year before the first Monday is considered as week 0
-
%x
- Current location's date representation
-
%X
- Current location's time representation
-
%y
- A year without century as a decimal number [00,99]
-
%Y
- A year with century as a decimal number
-
%Z
- The time zone name (no characters when there is no time zone)
-
%%
- A literal %
character
Conclusion:
In this post, we saw how the current date can be obtained using different methods. Don't forget to let us know which method you used in building your app, in the comment section below.
You may also like: