Write a Python program to drop microseconds from datetime


This program is a Python script that prints the current date and time.

  • The line dt = datetime.datetime.today().replace(microsecond=0) creates a datetime object using the datetime module. The datetime.today() method returns the current date and time as a datetime object. The replace method is then called on the resulting object to set the microseconds to 0.
  • Finally, the line print(dt) prints the datetime object, which contains the current date and time without microseconds.
  • The datetime module provides the datetime class for working with dates and times. The datetime class has various methods for manipulating and formatting dates and times, such as today(), replace(), and strftime().

Source Code

import datetime
dt = datetime.datetime.today().replace(microsecond=0)
print(dt)

Output

2022-09-24 12:38:42

Example Programs