spencermountain / spacetime

A lightweight javascript timezone library
http://spacetime.how/
Other
3.97k stars 185 forks source link

Rounding up .diff #344

Open owen95t opened 1 year ago

owen95t commented 1 year ago

I cant seem to find a way to do this, so I assume that currently it is impossible?

If so, it would be great to have this as a function.

Using this library to calculate penalty times by the hour, so it would be nice to have a round up function!

spencermountain commented 1 year ago

hey @owen95t i'd love to know more about your idea -

we currently have .nearest, which could be coerced like this:

let rounded = s.nearest('hour')
if(rounded.isBefore(s)){
  rounded=rounded.add(1, 'hour')
}

lemme know cheers

owen95t commented 1 year ago

@spencermountain

One of the options that I love about dayjs is that their diff can actually return a float.

So instead of the logic above, i can simply do a

let rounded = Math.ceil(x.diff(y, 'hour', true)) / / true option allows for returning a float in dayjs

But I am using spacetime due to how spacetime can manipulate time, which i think is better than dayjs in the application I am working on.

Using nearest is a good option as well, just thought that it would also be nice to get a float back for more manipulation