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

One year duration gives me back 1 year and 5 days ??? #1466

Closed
furier opened this issue Feb 6, 2014 · 5 comments
Closed

One year duration gives me back 1 year and 5 days ??? #1466

furier opened this issue Feb 6, 2014 · 5 comments

Comments

@furier
Copy link

furier commented Feb 6, 2014

Hi, I know its unnecessary to call asMilliseconds() on the duration and then make a new duration, but to mimic my code as closely as possible without cluttering the example with business logic i chose to keep it.

Also I have not tested just calling var mom = moment.duration({ y: 1 }); var days = mom.days();,
I figured first getting milliseconds with asMilliseconds() and then passing the result into a new duration may cause the bug.

var ms = moment.duration({ y: 1 }).asMilliseconds();
var mom = moment.duration(ms);
var year = mom.years(), 
    month = mom.months(), 
    days = mom.days(), 
    hours = mom.hours(), 
    minutes = mom.minutes(), 
    seconds = mom.seconds(), 
    milliseconds = mom.milliseconds();

The above gives me the following result:

year equals 1 and days equals 5

I don't understand why...?

@chall8908
Copy link

Based on my understanding of durations, this is because of weirdness in what equates to a month. Moment's durations define a month as being 30 days. However, this means that a year (12 months) has 360 instead of 365 days. Since your first duration knew it was supposed to be a year (365 days) it would have worked properly, but your second duration didn't. It consumed the milliseconds and converted upwards.

Whether or not this is intended behavior or even what's actually happening, I don't know. It does seem to be a plausible explanation, though.

@ichernev
Copy link
Contributor

Yeah. Your best bet is to create a duration with the right units (1 year is better than the ms duration). If you add a duration to moment we try to be smart, if not file a bug for that. Cloning a duration should preserve those properties, but dont go through ms - it ruins it all.

@furier
Copy link
Author

furier commented Feb 11, 2014

well I have a function which returns only ms, so when I want to know what the ms translates to I use moments duration, but get back incorrect results which is annoying :S

@chall8908
Copy link

You could try creating a moment from the milliseconds and comparing it to a moment at time 0. It's not a perfect solution, but it may give you more reliable results. Durations really aren't good for doing any kind of accurate measurements, particularly from milliseconds and even more particularly when those milliseconds span multiple months.

@icambron
Copy link
Member

icambron commented Mar 5, 2014

Closing for now. Maybe someday we'll get some nice logic that does precise rollups (like in countdown.js or Precise Diff), but I don't see a big need to address this now.

@icambron icambron closed this as completed Mar 5, 2014
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

4 participants