There are many ways to get the current date and time in Python using the built-in and third-party modules. The below steps show how to get the current date and time using the datetime and time module. Show
Example: Get Current DateTime in Python
Output: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'> As you can see in the output we got the current date and time in the following format. YYYY-MM-DD HH:MM:SS.MS Refer to Python DateTime Format Using Strftime() If you want to get the current date in various formats. Extract Current Date and Time Separately from a Datetime ObjectNote: in Python, The date and datetime are objects. So when we are manipulating date and time, that means we are actually dealing with objects. For example, you can extract the current date and time separately from a YYYY-MM-DD HH:MM:SS.MS3 object
Output: Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'> Break DateTime to Get Current Year, Month, Day, Hour, Minute, SecondsThe datetime module provides several attributes to access the induvial component such as a year, month, day, hour, minute, seconds. Example: In this example, we’ll break the current datetime and assign them into variables like the year, month, day, hour, minute, seconds, and microseconds.
Output: Year: 2021 Month: 7 Day = 16 Hour: 8 Minute: 28 Second: 0 Microsecond: 619640 Note: You can also use the 6 to break the datetime an get the induvial attribute for it.Get Current Date using the Date classPython Datetime module provides the Date class to represent and manipulate the dates. The Date class considers the Gregorian calendar.
Example:
Note: The 9 return the current date and time. This method is functionally equivalent to Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>0, but without timezone information. Get Current Time in PythonThere are many ways to get the current time in Python using the built-in and third-party modules. Python time module provides various functions to get the current time and perform time-related activities. We will see each one by one Current Time in Seconds Using # import only datetime class from datetime import datetime # current datetime now = datetime.now() current_date = now.date() print('Date:', current_date) print(type(current_date)) current_time = now.time() print('Time', current_time) print(type(current_time))0Use the 0 function to get the current time in seconds since the epoch as a floating-point number.This method returns the current timestamp in a floating-point number that represents the number of seconds since Jan 1, 1970, 00:00:00. It returns the current time in Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>3 format. Example:
You can use this timestamp to also. Current Time in MiliSeconds Using 0Current Time Using Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>5Use the Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>5 function to display the current time in a human-readable format. This function represents the current time in the operating system preferred way. The output may vary as per the operating system. Example:
Current Time Using Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>7Use the Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>7 function to return the current time expressed in seconds since the epoch to a local time in the a Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>9 format. You can access year, month, day, hour, minute, seconds, and microseconds from a Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>9. Example: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>0 Output: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>1 Get Current Time Using Datetime ModuleThe YYYY-MM-DD HH:MM:SS.MS5 method of a datetime class returns the current time in a human-readable format. It internally uses the Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>7 without the timezone info (if not given). Also, you can access the individual attribute such as hour, minutes, seconds, and microseconds Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>2 Output: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>3 Get Current Time in MillisecondsThere is no specific attribute or method in Python to get the current time in milliseconds. However, as milliseconds are three decimal places away from seconds, we can convert seconds to milliseconds by multiplying seconds by 1000.
Example: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>4 Get Current UTC TimeUTC – Coordinated Universal Time is the common time standard across the world. So, in Python, to work with the timezone without any issues, it is recommended to use the UTC as your base timezone.
Example: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>5 Output: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>6 Get Current Time in a Specific TimezoneUse the third-party pytz module to get the current time of any timezone. Steps:
Example: Refer to our guide on working with timezones in Python. Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>7 Output: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>8 Get Current GMT Time
Example: Current DateTime: 2021-07-16 19:17:20.536991 Type: <class 'datetime.datetime'>9 Output: YYYY-MM-DD HH:MM:SS.MS0 Get Current Time in ISO FormatUse the 9 method to get the current date and time in ISO format.Use Year: 2021 Month: 7 Day = 16 Hour: 8 Minute: 28 Second: 0 Microsecond: 6196400 method on a YYYY-MM-DD HH:MM:SS.MS5 instance to get the current date and time in the following ISO 8601 format: |