Can I easily get datetime with lower resolution in Python?

Obviously, I can get the date and time from datetime.datetime.now() , but I'm not really interested in seconds or especially microseconds.

Somewhere I can easily get Date + Hour + Minute?

+4
source share
1 answer

You can clear the second and microsecond component of the datetime value, for example:

 dt = datetime.datetime.now() #Now get rid of seconds and microseconds component: dt = dt.replace(second=0, microsecond=0) 

This will allow you to compare datetimes with minute detail.

If you want to simply print the date without a second / microsecond component, use the appropriate format string:

 dt = datetime.datetime.now() print dt.strftime("%Y/%m/%d %H:%M") >>> '2012/12/12 12:12' 
+12
source

All Articles