Matt Lightbourn
Matt Lightbourn

Reputation: 597

How do you truncate or round numbers by units NOT decimals?

I have dates and times in number format of YYYYMMDDHHMMSS. As a group by for a sum, I want to have the ability to group by:

YYYYMM

YYYYMMDD

YYYYMMDDHH

I have previously used this below, but it converts it into a string, which isn't great:

"sum_by_date":  { $substrBytes: [ "$_id.transaction_date", 0, 7 

] }

What I want to achieve from 20181217134218 for by month is either:

201812 or 20181200000000 and kept as a number format.

Thanks, Matt

Upvotes: 0

Views: 209

Answers (1)

aridlehoover
aridlehoover

Reputation: 3585

Use integer division to truncate the number. In Ruby:

(20181217134218 / 100000000) * 100000000  # = 20181200000000

Upvotes: 1

Related Questions