How to get min, seconds and milliseconds from datetime.now() in python?

Question:

>>> a = str(datetime.now())
>>> a
'2012-03-22 11:16:11.343000'

I need to get a string like that: '16:11.34'.

Should be as compact as possible.

Or should I use time() instead?
How do I get it?

Asked By: Prostak

||

Answers:

What about:

datetime.now().strftime('%M:%S.%f')[:-4]

I’m not sure what you mean by “Milliseconds only 2 digits”, but this should keep it to 2 decimal places. There may be a more elegant way by manipulating the strftime format string to cut down on the precision as well — I’m not completely sure.

EDIT

If the %f modifier doesn’t work for you, you can try something like:

now=datetime.now()
string_i_want=('%02d:%02d.%d'%(now.minute,now.second,now.microsecond))[:-4]

Again, I’m assuming you just want to truncate the precision.

Answered By: mgilson

Another similar solution:

>>> a=datetime.now()
>>> "%s:%s.%s" % (a.hour, a.minute, a.microsecond)
'14:28.971209'

Yes, I know I didn’t get the string formatting perfect.

Answered By: gdw2

This solution is very similar to that provided by @gdw2 , only that the string formatting is correctly done to match what you asked for – “Should be as compact as possible”

>>> import datetime
>>> a = datetime.datetime.now()
>>> "%s:%s.%s" % (a.minute, a.second, str(a.microsecond)[:2])
'31:45.57'
Answered By: Pythomania

time.second helps a lot put that at the top of your python.

Answered By: timothy timer

Sorry to open an old thread but I’m posting just in case it helps someone. This seems to be the easiest way to do this in Python 3.

from datetime import datetime

Date = str(datetime.now())[:10]
Hour = str(datetime.now())[11:13]
Minute = str(datetime.now())[14:16]
Second = str(datetime.now())[17:19]
Millisecond = str(datetime.now())[20:]

If you need the values as a number just cast them as an int e.g

Hour = int(str(datetime.now())[11:13])
Answered By: Adam Morris
import datetime from datetime

now = datetime.now()

print "%0.2d:%0.2d:%0.2d" % (now.hour, now.minute, now.second)

You can do the same with day & month etc.

Answered By: B.Welboren

if you want your datetime.now() precise till the minute , you can use

datetime.strptime(datetime.now().strftime('%Y-%m-%d %H:%M'), '%Y-%m-%d %H:%M')

similarly for hour it will be

datetime.strptime(datetime.now().strftime('%Y-%m-%d %H'), '%Y-%m-%d %H')

It is kind of a hack, if someone has a better solution, I am all ears

Answered By: Rajdeep Mukherjee

To the min, seconds and milliseconds, you can first import datetime.
Then you define a object with the datetime.datetime.now().
Then you can as that object for min, seconds and milliseconds, bu using the sub function of the class.

import datetime
now = datetime.datetime.now()
print (now.year)
print (now.month)
print (now.day)
print (now.hour)
print (now.minute)
print (now.second)
print (now.microsecond)

You can read more about her https://docs.python.org/3/library/datetime.html#datetime.datetime.year

Answered By: Eideen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.