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 will explore some of the most common methods used by developers for getting the current date and time in Python, including using the built-in datetime module.
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:
`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
Here is an example of using
`strftime` to get a formatted date and time string:
`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 (
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.
|Locale’s abbreviated weekday name.|
|Locale’s full weekday name.|
|Locale’s abbreviated month name.|
|Locale’s full month name.|
|Locale’s appropriate date and time representation.|
|Day of the month as a decimal number [01,31].|
|Hour (24-hour clock) as a decimal number [00,23].|
|Hour (12-hour clock) as a decimal number [01,12].|
|Day of the year as a decimal number [001,366].|
|Month as a decimal number [01,12].|
|Minute as a decimal number [00,59].|
|Locale’s equivalent of either AM or PM.|
|Second as a decimal number [00,61].|
|Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.|
|Weekday as a decimal number [0(Sunday),6].|
|Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.|
|Locale’s appropriate date representation.|
|Locale’s appropriate time representation.|
|Year without century as a decimal number [00,99].|
|Year with century as a decimal number.|
|Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].|
|Time zone name (no characters if no time zone exists).|
|A literal |
I hope these examples have helped you understand how to get the current date and time in Python, and how to convert between timezones.
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.
content is Very helpful sir !
Thnx, very helpful!
Every nicely explianed!