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

work out time left #511

Closed
seabannan opened this issue Nov 15, 2012 · 4 comments
Closed

work out time left #511

seabannan opened this issue Nov 15, 2012 · 4 comments

Comments

@seabannan
Copy link

What i am trying to do is do a countdown to work out the time left from now.

example i have

    var countDownTill = '2012-11-19 00:00:00 +0000';
    document.write(moment(countDownTill).diff(moment(), 'months') + " months<br>");
    document.write(moment(countDownTill).diff(moment(), 'weeks') + " weeks<br>");
    document.write(moment(countDownTill).diff(moment(), 'days') + " days<br>");

which will output

    0 months
    1 weeks
    4 days

but its not working out overall its doing it individuals for each value (days,weeks,months)

so if i up the date by say 12 months like below.

    var countDownTill = '2013-11-19 00:00:00 +0000';
     document.write(moment(countDownTill).diff(moment(), 'months') + " months<br>");   
     document.write(moment(countDownTill).diff(moment(), 'weeks') + " weeks<br>");
     document.write(moment(countDownTill).diff(moment(), 'days') + " days<br>");

it outputs.

    12 months
    53 weeks
    369 days

where as i am trying to get it to output

    12 months
    2 weeks
    5 days

example here
http://jsfiddle.net/fDmWH/3/

I posted on stack overflow and go some help but their still seems to be issues with weeks here is the solution we came to.

// Start date
var countDownTill = new Date('2013-11-19 00:00:00 +0000');
var now = new Date();

document.write(countDownTill + '<br />');

// Get the months
var months = moment(countDownTill).diff(moment(), 'months');
console.log(months);

// Add months to the date
now.setMonth(now.getMonth() + months);
document.write(now + '<br />');

// Get the weeks
var weeks = moment(countDownTill).diff(now, 'weeks');
// Seems like moment is doing something wrong here... it should be 0 weeks...
// 15 Nov to 19 Nov is 0 weeks to me at least...
console.log(weeks);

// Add the weeks to the date
now.setDate(now.getDate() + (7 * weeks));
document.write(now + '<br />');

var days = moment(countDownTill).diff(now, 'days');

document.write(months + ' months ');
document.write(weeks + ' weeks ');
document.write(days + ' days ');

http://jsfiddle.net/fDmWH/6/

@seabannan
Copy link
Author

Really need help with this it always seems to end up 1 day out any suggestions?

@ichernev
Copy link
Contributor

You should pass true as a third argument to diff to disable rounding, and do Math.floor instead.

Check this: http://jsfiddle.net/x5LnX/1/

@seabannan
Copy link
Author

ok thanks ;)

@timrwood
Copy link
Member

It may be better to use the duration object for this.

var diff = moment('2012-11-19T00:00:00+0000').diff(moment()); // in milliseconds
var duration = moment.duration(diff);

console.log(duration.years() + ' years');               // 0 years
console.log(duration.months() + ' months');             // 0 months
console.log(duration.days() + ' days');                 // 2 days
console.log(duration.hours() + ' hours');               // 6 hours
console.log(duration.minutes() + ' minutes');           // 43 minutes
console.log(duration.seconds() + ' seconds');           // 41 seconds
console.log(duration.milliseconds() + ' milliseconds'); // 250 milliseconds

Formatting durations has been addressed a few times, see #463 and #264

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