Python Current Date Time With Examples

We can use Python datetime module to get the current date and time of the local system.

Output: 2018-09-12 14:17:56.456080

Python Current Date

If you are interested only in the date of the local system, you can use the datetime date() method.

Output: 2018-09-12

Python Current Time

If you want only time in the local system, use time() method by passing datetime object as an argument.

Output: 14:19:46.423440

Python Current Date Time in timezone – pytz

Most of the times, we want the date in a specific timezone so that it can be used by others too. Python datetime now() function accepts timezone argument that should be an implementation of tzinfo abstract base class.

Python pytz is one of the popular module that can be used to get the timezone implementations.

You can install this module using the following PIP command.

Let’s look at some examples of using pytz module to get time in specific timezones.

Output:

If you want to know all the supported timezone strings, you can print this information using the following command.

It will print the list of all the supported timezones by the pytz module.

Python Pendulum Module

Python Pendulum module is another timezone library and according to its documentation, it is faster than the pytz module.

We can install Pendulum module using below PIP command.

You can get list of supported timezone strings from pendulum.timezones attribute.

Let’s look into some examples of getting current date and time information in different time zones using the pendulum module.

Output:

You can checkout complete python script and more Python examples from our GitHub Repository.

By admin

Leave a Reply