How To Get Current Date Time or Now In Python?
Date and time is important part of the applications development. We generally need to set, get and change some data time information to our records. In this tutorial we will examine the python date time now functions and use cases with examples. For more information about Python date time functions can be get from following tutorial.
In order to use datetime functionalities weshould import the module named
datetime . We can import
datetime module like below.
datetime module we can use provided class and functions. The simplest and most basic way to get current date and time is using
datetime.now() function. This resides inside
datetime module which means we should use following code in order to get current date and time.
Convert Now To String
now() function returned data will be a datetime object. But in some situations we may need to convert date time into string to use string variable or print to the console. We can use
str() functions which will convert current date time to string.
Print Now with GM Time
We can use
gmtime() function which will print current GM time. GM time is the current time in Greenwich where the world time starts and ends. We should
import time module in order to use
Print Specific Part Of Current Time
In this part we will print only specific part of the current time. We will use specifiers in order to print only required part of the time. We assume we have get the current time into variable named
now like below.
now = datetime.datetime.now()
Print Current Second
Print Current Minute
Print Current Hour
Print Current Day of Month
Print Current Month
Print Current Year