How to Get Current Date & Time in Python – TecAdmin

Getting the current date and time is a common task in Python programming. There are several ways to do this, depending on your needs and the libraries you have available. In this article, we’ll explore some of the most common methods used by developers to get the current date and time in Python, including using the built-in datetime module.

To learn more about it, visit our detailed Python date and time tutorial.

Get the current date and time in Python To get the current date and time, you can use the ‘datetime.now

()’ function, which returns a datetime object that represents the current

date and time in the local time zone.

The above script will generate the following

:2022-12-25 18:13:13.363653 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 date and time values should be formatted.

for example to get the current date as a string in the format ‘y-mm-dd hh:mm:ss’ you can use the format string

‘%Y-%m-%d %H:%M:%S’:The

above script will generate the following

:2022-12-25 18:15:03 Python ‘datetime

‘ module class attributes The Python datetime module

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 time of 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 date and time

formatting in Python To get a date and time string formatted 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 an 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 time 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 time in 12-hour format with a leading zero for single-digit hours.

Here are some 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.

PolicyMeaning%aAbbreviated name of the day of the week of the locality.%Full name of the day of the week of ALocale.%bShort name of the month of Locale.%cFull name of the month of BLocale.%cProper date and time representation of Locale.%dDay of the month as decimal number [01,31].%HHour (24-hour clock) as decimal number [00,23].%IHour (12-hour clock) as decimal number [01,12].%jDay of the year as decimal number [001,366].%mMonth as number decimal [01,12].%MMinute as decimal number [00,59].%pLocal equivalent of either AM or PM.%SSecond as decimal number [00,61].%UWeek number of the year (Sunday as first day of the week) as decimal number [00,53]. All days of a new year preceding the first Sunday are considered in week 0.%wWeekday as decimal number [0(Sunday),6].%WWeek number of the year (Monday as first day of the week) as decimal number [00,53]. All days of a new year prior to the first Monday are considered in week 0.%xLocale appropriate date representation.%XLocale’s appropriate time representation.%yYear without century as a decimal number [00,99].%YYear with century as a decimal number.%zTime zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal time digits and M represents decimal minute digits [-23:59, +23:59].%ZTime zone name (no characters if no time zone exists).%%A literal character ‘%’.

I hope these examples helped you understand how to get the current date and time in Python and how to convert between time zones.

Conclusion

In short, 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 time zone, or the datetime.utcnow() function to get the current date and time in the UTC time zone. You can also use the datetime() constructor to create a datetime object for a specific date and time.