How to round off timestamp in milliseconds to nearest seconds?

AKIWEB picture AKIWEB · Dec 4, 2013 · Viewed 20.6k times · Source

How to round off the current timestamp in milliseconds to seconds?

If this is the current timestamp in milliseconds I have -

1384393612958

The if I am rounding off to nearest second then will it be like this?

Time in MS rounded off to nearest Second = 1384393612000

I might need to do this both in Java and C++.

Answer

Suman picture Suman · Dec 4, 2013

If you are using Python:

old_number = 1384393612958
new_number = 1000 * (old_number / 1000)

print new_number

Basically you want to use an integer number, divide by one thousand (to shave off the milli-seconds), and then multiple by thousand to get the ms value rounded to seconds.