now() gives me
datetime.datetime(2010, 7, 6, 5, 27, 23, 662390)
How do I get just datetime.datetime(2010, 7, 6, 5, 27, 0, 0)
(the datetime object) where everything after minutes is zero?
If you just want strings, you could remove the trailing seconds with a regex ':\d\d$' .
Using strfttime to Remove the Time from Datetime in Python We can use strftime() to easily remove the time from datetime variables. For example, if you want to print out the date in the format “YYYY-MM-DD”, we pass “%Y-%m-%d” to strfttime() and no time is printed.
Try print(f"{now. hour}:{now. minute}") . Note that this will not add leading zeros or other useful time formatting, but that can be added trivially by referencing the documentation for formatting strings.
dtwithoutseconds = dt.replace(second=0, microsecond=0)
http://docs.python.org/library/datetime.html#datetime.datetime.replace
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With