Quick Reach
- 1 Python datetime module
- 2 We have Video of this tutorial
- 3 Python date and time formatting examples
- 3.1 An example of current time and date
- 3.2 Getting current day example
- 3.3 Getting current month number example
- 3.4 Getting current month name with full month name
- 3.5 Getting current month abbreviation
- 3.6 Getting Current year in Python
- 3.7 Number of day of the Week
- 3.8 Getting name of the day
- 3.9 Current date in dd/mm/yyyy example
- 3.10 Format date in day name and month name example
- 3.11 Python Current time example (hh: mm :ss)
- 3.12 Display week number of the year
- 3.13 Display current date
- 4 Related
Python datetime module
Python comes up with a few date and time modules. One of those is the datetime module. In order to format the date and time in your programs, you have to import the datetime module into your programs.
We have Video of this tutorial
This is how you may include the datetime Python module in your program:
Importing datetime module:
import datetime
Python date and time formatting examples
Following are a few date and time formatting examples in Python. We will use the datetime properties to format the date and time. See examples with the code below.
An example of current time and date
The following example will return the current date and time by using the datetime module. The daytime’s now method is used to return the current time and date.
1
2
3
4
5
|
import datetime
dtatt = datetime.datetime.now()
print (“Current: “ , dtatt)
|
Getting current day example
Following example uses the datetime.date to return the current day. Note that, the strftime is the string representation of the date, used in the examples below.
Also, the output shown in the example pages is related to the dates when these demo pages were created.
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%d”))
|
Getting current month number example
You may embed the ‘%m’ directive to get the current month number. For example, 03 for March.
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%m”))
|
Getting current month name with full month name
To get full the month name like October, November etc. use the %B directive.
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%B”))
|
Getting current month abbreviation
For the abbreviation of the Month name like Sep, Oct, Dec etc. use the ‘%b’ in the formatted string.
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%b”))
|
Getting Current year in Python
See this example where the current Year is displayed by using the datetime module:
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%Y”))
|
Note: Use the ‘%y’ to get the year in two digits.
Number of day of the Week
For getting the number of the day in a Week, use the %w. In that case, the 1 is for Monday, 2 for Tuesday and so on.
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%w”))
|
Getting name of the day
You can use ‘%A’ directive to get the name of the day like Monday, Tuesday, Wednesday etc. as shown in the example below:
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%A”))
|
Current date in dd/mm/yyyy example
The following example uses Python datetime module to display the current date in dd/mm/yyyy format.
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%d/%m/%Y”))
|
Format date in day name and month name example
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%A, %d %B,%Y”))
|
Python Current time example (hh: mm :ss)
Following example displays the current time using datetime module.
1
2
3
4
5
|
import datetime
currenttime = datetime.datetime.now()
print (“%s:%s:%s” % (currenttime.hour, currenttime.month, currenttime.second) )
|
Display week number of the year
For getting the number of Week in the year, use the %U as shown in the code below:
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%U”))
|
Display current date
Display the current date by using the ‘%x’:
1
2
3
|
import datetime
print (datetime.date.today().strftime(“%x”))
|
Further Reading: The Python time Module
Leave A Comment?