You can use datetime.replace() method -
>>> d = datetime.datetime.today().replace(microsecond=0)
>>> d
datetime.datetime(2015, 7, 18, 9, 50, 20)
Answer from Anand S Kumar on Stack OverflowHow do i remove milliseconds?
Removing milliseconds from datetime object in Python - Stack Overflow
python - Format a datetime into a string with milliseconds - Stack Overflow
python - Strip microsecond from datetime - Stack Overflow
In my django project i need to display duration of quiz test. In my model i have two datetime fields:
create_timestamp = models.DateTimeField(auto_now_add=True) update_timestamp = models.DateTimeField(auto_now=True)
I've made a property method to display duration:
@property
def duration(self):
return self.update_timestamp - self.create_timestampIn HTML output is like this: 0:02:09.099502
I wanna get rid of milliseconds. Please help \_o_O_/
You already have a datetime object, you do not need to parse it again. The datetime.fromtimestamp() call was enough.
Remove the datetime.strptime() line.
created_date = datetime.fromtimestamp(ctime)
created_date = created_date.strftime("%m/%d/%Y %I:%M:%S %p")
print(created_date)
I also changed your strftime() call, it is a method, you just call it on the datetime object you have.
I suspect that you printed the return value of the datetime.fromtimestamp() call, and got confused. The str() conversion of a datetime() instance formats the value as a ISO 8601 string. Note that even if you did have a string, you used the wrong format (there is no timezone in that string, so %Z does not apply).
If you needed a datetime object, rather than a formatted string, you could also just have converted your timestamp to an integer; the microseconds are captured in the decimal portion of the timestamp:
>>> ctime = 1505252035.28109
>>> datetime.fromtimestamp(ctime)
datetime.datetime(2017, 9, 12, 22, 33, 55, 281090)
>>> datetime.fromtimestamp(int(ctime))
datetime.datetime(2017, 9, 12, 22, 33, 55)
>>> print(_)
2017-09-12 22:33:55
You can use time as well to achieve what you want.
import time
ctime = "2017-09-12 22:33:55.28109"
x = time.strptime(ctime.split('.')[0],'%Y-%m-%d %H:%M:%S')
x = time.strftime('%m/%d/%Y %I:%M:%S %p', x)
print (x)
'09/12/2017 10:33:55 PM'
To get a date string with milliseconds, use [:-3] to trim the last three digits of %f (microseconds):
>>> from datetime import datetime
>>> datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
'2022-09-24 10:18:32.926'
Or shorter:
>>> from datetime import datetime
>>> datetime.utcnow().strftime('%F %T.%f')[:-3]
'2022-09-24 10:18:32.926'
See the Python docs for more "%" format codes and the strftime(3) man page for the full list.
With Python 3.6+, you can set isoformat's timespec:
>>> from datetime import datetime
>>> datetime.utcnow().isoformat(sep=' ', timespec='milliseconds')
'2019-05-10 09:08:53.155'
Let's say you have a mix of different formats that looks like this:
import pandas as pd
df = pd.DataFrame()
df['time'] = ['2018-06-01 06:36:40.047883+00:00', '2018-06-01 06:36:40.047883+00:00', '2018-06-04 11:30:00+00:00', '2018-06-01 06:36:40.047883']
Corresponding output:
time
0 2018-06-01 06:36:40.047883+00:00
1 2018-06-01 06:36:40.047883+00:00
2 2018-06-04 11:30:00+00:00
3 2018-06-01 06:36:40.047883
You wish to get to a common format by removing microseconds and anything after +. In short, you want something that is in Y-M-D H-M-S format.
Currently, let me assume that your column is in string format. So, we now convert this to a datetime format and then replace the microseconds part with 0 and get rid of it.
df['time'] = pd.to_datetime(df['time'])
df['time'] = df['time'].apply(lambda x: x.replace(microsecond = 0))
Output:
time
0 2018-06-01 06:36:40
1 2018-06-01 06:36:40
2 2018-06-04 11:30:00
3 2018-06-01 06:36:40
Another way to achieve that is by using str.split:
t = "2018-06-04 11:30:00+00:00"
t.split('+')[0]
dtwithoutseconds = dt.replace(second=0, microsecond=0)
http://docs.python.org/library/datetime.html#datetime.datetime.replace
I know it's quite old question, but I haven't found around any really complete answer so far.
There's no need to create a datetime object first and subsequently manipulate it.
dt = datetime.now().replace(second=0, microsecond=0)
will return the desired object