Quick Reach
- 1 The time module in Python
- 2 Formatting date and time examples
- 2.1 Current time and date with %c example
- 2.2 Full weekday name example
- 2.3 Display current short month name
- 2.4 Display current full month name example
- 2.5 Display current month as number
- 2.6 Display current day of month as number example
- 2.7 Python current hour in 24 hours format
- 2.8 Display current hour as 12 hours clock
- 2.9 Display weekday of the Week in number
- 2.10 Display current date
- 2.11 Python current time
- 2.12 Display year without century
- 2.13 Display year with century
- 2.14 Display time zone
The time module in Python
The time module of Python is used to format date and time. In order to use the time module in Python programs, this has to be included. This chapter explains how to use Python time module but let us first look at how you can import it.
This is how you will import the time module into Python project:
Import time
General syntax of using time module
time.strftime(format)
Where Format is the directive you can use like %c, %a, %A etc.
Examples of time and date formatting using time, Python module are given below.
Formatting date and time examples
Following are examples using time module with formatting characters to display formatted date and time. Note that, for our examples we are using 12/01/2013 date.
Current time and date with %c example
To get the current time and date you can use ‘%c’ directive as follows:
1
2
3
4
5
|
import time
formateddate = time.strftime(“%c”)
print (formateddate)
|
Full weekday name example
To get the full Weekday name like Sunday, Monday etc. use the ‘%A’ formatting character.
1
2
3
4
5
|
import time
formateddate = time.strftime(“%A”)
print (formateddate)
|
Display current short month name
If you need to display the month in short format like Jan, Feb, Mar etc. then use the ‘%b’ directive, as shown in the example below:
1
2
3
4
5
|
import time
formateddate = time.strftime(“%b”)
print (formateddate)
|
Display current full month name example
To get the full Month name you can use ‘%B’. See example:
1
2
3
4
5
|
import time
formateddate = time.strftime(“%B”)
print (formateddate)
|
Display current month as number
Small letter i.e. ‘m’ is used to return the current month in Python time module. See example below.
1
2
3
4
5
|
import time
formateddate = time.strftime(“%m”)
print (formateddate)
|
Display current day of month as number example
Following example uses the time module to display the current day of the month as a number.
1
2
3
4
5
|
import time
formateddate = time.strftime(“%d”)
print (formateddate)
|
Python current hour in 24 hours format
Use ‘%H’ to get the current hour in 24-hour clock format.
1
2
3
4
5
|
import time
formateddate = time.strftime(“%H”)
print (formateddate)
|
Display current hour as 12 hours clock
This example uses time with ‘%I’ to return current hour in 12 hours format.
1
2
3
4
5
|
import time
formateddate = time.strftime(“%I”)
print (formateddate)
|
Display weekday of the Week in number
To get the number of day in the Week, use ‘%w’, Where:
1= Monday
0 = Sunday
1
2
3
4
5
|
import time
formateddate = time.strftime(“%w”)
print (formateddate)
|
Display current date
To get and display only the current date, unlike in above example where we displayed the date and time, use the ‘%x’ directive.
1
2
3
4
5
|
import time
formateddate = time.strftime(“%x”)
print (formateddate)
|
Python current time
Following example returns the current time using by using the ‘%X’.
1
2
3
4
5
|
import time
formateddate = time.strftime(“%X”)
print (formateddate)
|
Display year without century
1
2
3
4
5
|
import time
formateddate = time.strftime(“%y”)
print (formateddate)
|
Display year with century
1
2
3
4
5
|
import time
formateddate = time.strftime(“%Y”)
print (formateddate)
|
Display time zone
Following example returns timezone using ‘%z’ with the time module.
1
2
3
4
5
|
import time
formateddate = time.strftime(“%Z”)
print (formateddate)
|
Leave A Comment?