How To Get Current Date Time or Now In Python?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/get-current-date-time-or-now-in-python/
PINTEREST
PINTEREST
INSTAGRAM

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.

Python Date Time Functions With Examples

Python Date Time Operations

Import datetime

In order to use datetime functionalities weshould import the module named datetime . We can import datetime module like below.

datetime.now() Function

After importing 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.

datetime.now() Function

datetime.now() Function

Convert Now To String

While using 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.

Convert Now To String

Convert Now 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 gmtime() function.

LEARN MORE  Python Command Line Arguments with sys.argv

Print Now with GM Time

Print Now with GM Time

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.

Print Current Second

Print Current Minute

Print Current Hour

Print Current Day of Month

Print Current Month

Print Current Year

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/get-current-date-time-or-now-in-python/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)