user194919
user194919

Reputation:

Convert a number (of days) to days, months and years with jQuery

I have a calculation function and part of this shows the number of days it will take to achieve a goal.

Rather than just show the number of days I want to calculate this into days & months or days, months and years depending on the number. I have an if statement for the splitting but can't seem to work out the maths to go from for example 132 days to x days x months... Any suggestions?

// GOAL
var timeToGoal = Math.round(goal / costPerDay);         

// if more than a year
if ( timeToGoal >= 365 ) {
    alert('days + months + years');

    // if more than a month but less than a year
} else if ( timeToGoal >= 30 && timeToGoal <=365 ) {
    alert('Days + months');
} else {
    alert('days');
    $('#savings-goal span').text(timeToGoal+' days');
}

Upvotes: 9

Views: 15947

Answers (3)

Matt
Matt

Reputation: 75317

Try something like this;

function humanise (diff) {
  // The string we're working with to create the representation
  var str = '';
  // Map lengths of `diff` to different time periods
  var values = [[' year', 365], [' month', 30], [' day', 1]];

  // Iterate over the values...
  for (var i=0;i<values.length;i++) {
    var amount = Math.floor(diff / values[i][1]);

    // ... and find the largest time value that fits into the diff
    if (amount >= 1) {
       // If we match, add to the string ('s' is for pluralization)
       str += amount + values[i][0] + (amount > 1 ? 's' : '') + ' ';

       // and subtract from the diff
       diff -= amount * values[i][1];
    }
  }

  return str;
}

It's expected that the argument is the difference in days you want to represent. It assumes a month of 30 days and a year of 365.

You should be using it like this;

$('#savings-goal span').text(humanise(timeToGoal));

http://jsfiddle.net/0zgr5gfj/

Upvotes: 11

JuanFernandoz
JuanFernandoz

Reputation: 799

This is a simpler solution that I did without the for bucle:

function jarh(x) {
var y = 365;
var y2 = 31;
var remainder = x % y;
var casio = remainder % y2;
year = (x - remainder) / y;
month = (remainder - casio) / y2;

var result ="--- Year ---" + year + "--- Month ---" + month + "--- Day ---" + casio;

return result;
}

var call = jarh(6781);

http://jsfiddle.net/yHAcY/1/

Upvotes: 2

You Qi
You Qi

Reputation: 9211

an attempt from me (this take leap year into account and based on the current date)

function humanise(total_days)
{
    //var total_days = 1001;
    var date_current = new Date();
    var utime_target = date_current.getTime() + total_days*86400*1000;
    var date_target = new Date(utime_target);

    var diff_year  = parseInt(date_target.getUTCFullYear() - date_current.getUTCFullYear());
    var diff_month = parseInt(date_target.getUTCMonth() - date_current.getUTCMonth());
    var diff_day   = parseInt(date_target.getUTCDate() - date_current.getUTCDate());

    var days_in_month = [31, (date_target.getUTCFullYear()%4?29:28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    var date_string = "";
    while(true)
    {
        date_string = "";
        date_string += (diff_year>0?diff_year + "Y":"");

        if(diff_month<0){diff_year -= 1; diff_month += 12; continue;}
        date_string += (diff_month>0?diff_month + "M":"");

        if(diff_day<0){diff_month -= 1; diff_day += days_in_month[((11+date_target.getUTCMonth())%12)]; continue;}
        date_string += (diff_day>0?diff_day + "D":"");
        break;
    }
    console.log(date_string);
    return date_string;
}

var timeToGoal = 1001;
$('#savings-goal span').text(humanise(timeToGoal));

Upvotes: 5

Related Questions