Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
To get the current date and time in Python, you can use the datetime
module from the Python standard library. Specifically, you can use the datetime.datetime.now()
function to get the current date and time as a datetime
object.
Here's an example:
import datetime # Get the current date and time current_datetime = datetime.datetime.now() # Print the current date and time print("The current date and time is:", current_datetime)
In this example, we import the datetime
module and use the datetime.datetime.now()
function to get the current date and time. The result is a datetime
object representing the current date and time, which we print.
If you only need the current date (without the time), you can use the datetime.date.today()
function:
import datetime # Get the current date current_date = datetime.date.today() # Print the current date print("The current date is:", current_date)
In this example, we use the datetime.date.today()
function to get the current date as a date
object, which we print.
Retrieve current date and time in Python:
from datetime import datetime current_datetime = datetime.now() print(f"Current date and time: {current_datetime}")
Python time
module for current date and time:
import time current_time = time.time() print(f"Current date and time in seconds since the epoch: {current_time}")
Current date and time in ISO format using Python:
from datetime import datetime current_iso_datetime = datetime.now().isoformat() print(f"Current date and time in ISO format: {current_iso_datetime}")
Python strftime()
for formatting current date and time:
from datetime import datetime current_formatted_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S") print(f"Formatted current date and time: {current_formatted_datetime}")
Get UTC time in Python using datetime
module:
from datetime import datetime, timezone current_utc_time = datetime.now(timezone.utc) print(f"Current UTC time: {current_utc_time}")
Python calendar
module for current date and time:
import calendar current_calendar = calendar.timegm(time.gmtime()) print(f"Current date and time using calendar module: {current_calendar}")
Using time.localtime()
for current date and time in Python:
import time current_local_time = time.localtime() print(f"Current date and time using time.localtime(): {current_local_time}")
Current date and time with milliseconds in Python:
from datetime import datetime current_datetime_with_ms = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") print(f"Current date and time with milliseconds: {current_datetime_with_ms}")
Python arrow
library for getting current date and time:
import arrow current_arrow_time = arrow.now() print(f"Current date and time using arrow library: {current_arrow_time}")
Current date and time in a different timezone with pytz
in Python:
from datetime import datetime import pytz tz = pytz.timezone("America/New_York") current_time_in_ny = datetime.now(tz) print(f"Current date and time in New York timezone: {current_time_in_ny}")
Python pendulum
library for handling date and time:
import pendulum current_pendulum_time = pendulum.now() print(f"Current date and time using pendulum library: {current_pendulum_time}")
Get current date in YYYY-MM-DD format in Python:
from datetime import date current_date = date.today() print(f"Current date in YYYY-MM-DD format: {current_date}")
Get current date and time as a timestamp in Python:
from datetime import datetime timestamp = datetime.timestamp(datetime.now()) print(f"Current date and time as a timestamp: {timestamp}")