0

How do I round a datetime variable like this one:

x = datetime.now()

which produces something like this:

2022-05-04 15:36:01.055696

to something like this?

2022-05-04 15:36:01.057
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Catarina Ribeiro
  • 562
  • 2
  • 16
  • Do you have a string or a `datetime` variable? The one you show is just a string. – Marco Bonelli May 04 '22 at 14:45
  • 1
    1) Rounding would produce `....056`. 2) Do you really need millisecond precision for a timestamp that includes the year? I would just trim the string with `x = x[:-3]` and call it a day (pun not intended). – chepner May 04 '22 at 14:47
  • x is datetime.now() – Catarina Ribeiro May 04 '22 at 14:47
  • Are you asking how to format the *string* so that it only shows 3 decimals of precision? Or are you asking how to round the *value*, so it would represent `2022-05-04 15:36:01.056000`? – 0x5453 May 04 '22 at 14:51
  • Does this answer your question? [datetime: Round/trim number of digits in microseconds](https://stackoverflow.com/questions/11040177/datetime-round-trim-number-of-digits-in-microseconds) – High-Octane May 04 '22 at 14:53
  • @High-Octane I think OP wants to modify the datetime object itself, rather than just format it. – Marco Bonelli May 04 '22 at 15:05

4 Answers4

2

In case you want to modify a datetime object, you can round its .microsecond attribute:

x = datetime.now()
print(x.isoformat(' '))
# 2022-05-04 15:36:01.055696

x = x.replace(microsecond=round(x.microsecond / 1000) * 1000)
print(x.isoformat(' '))
# 2022-05-04 15:36:01.056

In any case (even without modifying the object and rounding the microseconds), to get the representation you want you can format it with the appropriate method:

x.isoformat(' ', timespec='milliseconds')
# 2022-05-04 15:36:01.056

Note that modifying the object using round(x.microsecond / 1000) * 1000 for the microseconds will round them to the closest millisecond. If you just want to truncate the microseconds instead, use x.microsecond // 1000 * 1000.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
1

If you just want to remove the last few characters you could do:

print(x.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])

The formatting str is the default which is what you are seeing in the output

Z Li
  • 4,133
  • 1
  • 4
  • 19
1

You can convert it to a unix timestamp and then round the number to 3 decimal places.

from datetime import datetime

x = '2022-05-04 15:36:01.055696'

# Convert to datetime object
d = datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f')

# Convert to rounded unix timestamp
ts = datetime.fromtimestamp(round(d.timestamp(), 3))

# You know have rounded datetime object, you can convert back to string with
ts.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
-1

Pandas timestamp has a built in-function for this. Make the timestamp a timestamp object then use round.

import pandas as pd

x = '2022-05-04 15:36:01.055696'
ts = pd.Timestamp(x)
ts_rounded = ts.round(freq='L')
print(ts_rounded) 2022-05-04 15:36:01.056000
Alicia
  • 121
  • 1
  • 12