Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping off the seconds in datetime python

Tags:

python

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?

like image 631
Vishal Avatar asked Jul 06 '10 05:07

Vishal


People also ask

How do I remove the seconds from a datetime column in Python?

If you just want strings, you could remove the trailing seconds with a regex ':\d\d$' .

How do I get rid of hours minutes and seconds in datetime in Python?

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.

How do you get the current time without seconds in Python?

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.


1 Answers

dtwithoutseconds = dt.replace(second=0, microsecond=0) 

http://docs.python.org/library/datetime.html#datetime.datetime.replace

like image 193
ʇsәɹoɈ Avatar answered Oct 03 '22 22:10

ʇsәɹoɈ