Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duration doesn't work as expected #781

Closed
AjaySewradj opened this issue Sep 19, 2020 · 2 comments
Closed

Duration doesn't work as expected #781

AjaySewradj opened this issue Sep 19, 2020 · 2 comments

Comments

@AjaySewradj
Copy link

AjaySewradj commented Sep 19, 2020

I'm trying to use the Duration object to create a countdown clock by using the minus function to subtract a second. But it seems that the minus function operates on the units (e.g. seconds) independently.

Steps to reproduce

var duration = Duration.fromObject({minutes: 1, seconds: 0})
var time = `${duration.minutes} : ${duration.seconds.toString().padStart(2, '0')}`

var newDuration = duration.minus({seconds: 1});
var newTime = `${duration.minutes} : ${duration.seconds.toString().padStart(2, '0')}`

Expected:
time: 1:00
newTime: 0:59

Actual:
time: 1:00
newTime: 1:-1

@icambron
Copy link
Member

Yes, each unit is independent. Switch your third line to this in order to "reshuffle" the units:

var newDuration = duration.minus({seconds: 1}).normalize();

@RemyMachado
Copy link

@icambron
It seems the behavior is inconsistent. If I had to guess I would say that if the upper unit is present, then it's automatically subtracting from it, if not, then it goes into negative.
Please, check this codesandbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants