To understand more about it, visit our in-depth Python date time tutorial.

Get Current Date & Time in Python

To get the current date and time, you can use the datetime.now() function, which returns a datetime object representing the current date and time in the local timezone.

The above script will output the following: To format the date and time in a specific way, you can use the strftime() method of the datetime object, which takes a format string as an argument. The format string specifies how the date and time values should be formatted. For example, to get the current date as a string in the YYYY-MM-DD HH:MM:SS format, you can use the %Y-%m-%d %H:%M:%S format string:

The above script will output the following:

Python datetime Module Class Attributes

The datetime module in Python provides classes for working with dates, times, and timestamps. The main class is datetime, which represents a single point in time. Here are some common attributes of the datetime class:

year: the year (four digits) month: the month (1-12) day: the day of the month (1-31) hour: the hour of the day (0-23) minute: the minute of the hour (0-59) second: the second of the minute (0-59) microsecond: the microsecond of the second (0-999999) tzinfo: an object representing the time zone

Here is an example of creating a datetime object and accessing its attributes:

Get Formated Date Time in Python

To get a formatted date and time string in Python, you can use the strftime method of the datetime class. Here is an example of using strftime to get a formatted date and time string:

The strftime method takes a format string as its argument, which specifies how the date and time should be formatted. In the example above, the format string “%Y-%m-%d %H:%M:%S” specifies that the year should be formatted as a four-digit number (%Y), the month as a two-digit number (%m), the day as a two-digit number (%d), the hour as a two-digit number (%H), the minute as a two-digit number (%M), and the second as a two-digit number (%S). You can use different format codes to customize the output of strftime to your liking. For example, you can use %A to get the full name of the day of the week, %B to get the full name of the month, and %I to get the hour in 12-hour format with a leading zero for single-digit hours. Here is a few more examples of strftime format codes:

Below is the list of directives that can be used to format date and time output in your Python script. I hope these examples have helped you understand how to get the current date and time in Python, and how to convert between timezones.

Conclusion

In summary, the datetime module is a convenient and powerful tool for working with dates, times, and timestamps in Python. You can use the datetime.now() function to get the current date and time in the local timezone, or the datetime.utcnow() function to get the current date and time in the UTC timezone. You can also use the datetime() constructor to create a datetime object for a specific date and time.