>>> 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?
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.
[:-4]
means "start at the beginning and exclude the last 4 characters". For some reason, %f
isn't working here and my final result is 36:31.f
truncated to 36:
. @Prostak may also be getting a truncated result...
datetime.now().strftime('%M:%S.%f')[:-4]
I get ''
. If I use datetime.now().strftime('%M:%S.f')[:-4]
, I get '46:'
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'
strftime()
.... I just can't figure out the parameter for milliseconds datetime.now().strftime('%M:%S.??')
. maybe you know?
>>>import datetime >>>datetime.datetime.now().strftime("%M:%S.%f")[:-4] '14:07.90'
Mar 23, 2012 at 16:17
strftime()
......... it is just the one that provides one line solution
>>> "%s:%s.%s" % (datetime.datetime.now().minute, datetime.datetime.now().second, str(datetime.datetime.now().microsecond)[:2])
'21:51.16'
This does make calling datetime.now() redundant but I don't think there is a time difference in the three different function calls. Please correct me if I am wrong.
Mar 23, 2012 at 19:23
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
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.
strftime()
.... I just can't figure out the parameter for milliseconds datetime.now().strftime('%M:%S.??')
. maybe you know?
%d
instead of %s
because you are dealing with integer values (digits).
Apr 13, 2015 at 0:17
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.
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
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])
16:11.34
, or something else?