How To Get Current Date Time with Now() Function In Python? – POFTUT

How To Get Current Date Time with Now() Function In Python?


Date and time is an important part of application 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 the following tutorial.

Python Date Time Functions With Examples

Python Date Time Operations

Import datetime Module To Use datetime Functions

In order to use datetime functionalities, we should import the module named datetime . We can import datetime module like below.

import datetime

Print Now with datetime.now() Function

After importing datetime module we can use provided class and functions. The simplest and most basic way to get the current date and time is using datetime.now() function. This resides inside datetime module which means we should use the following code in order to get the current date and time.

datetime.datetime.now()
Print Now with datetime.now() Function

We can see that every time we execute the now() function it returns different date time information where generally the second changes. The now() function returns date time infomation in datetime format with (YYYY, M, D, H,M,S) format.

Convert Now (Current Time) 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 a string to use string variable or print to the console. We can use str() functions that will convert current date time to string.

str(datetime.datetime.now())

Print Now (Current Time) According To GM (Greenewitch Meantime)

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.

import time 
time.gmtime()
Print Now with GM Time
Print Now with GM Time

Print Specific Part Of Now (Current Time)

In this part, we will print an only specific part of the current time. We will use specifiers in order to print only the required part of the time. We assume we have got the current time into a variable named now like below.

now = datetime.datetime.now()

Print Current Second

We can print current second like below.

now.second

Print Current Minute

We can print current minute like below.

now.minute

Print Current Hour

We can print current hour like below.

now.hour

Print Current Day of Month

We can print current day of month like below.

now.day

Print Current Month

We can print current month like below.

now.month

Print Current Year

We can print current year like below.

now.year

LEARN MORE  What Is Socket In Linux?

2 thoughts on “How To Get Current Date Time with Now() Function In Python?”

  1. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.

    Reply
  2. Pingback: Google

Leave a Comment