2012-01-20 21 views
9

Ho una funzione di calcolo e una parte di questo mostra il numero di giorni necessari per raggiungere un obiettivo.Convertire un numero (di giorni) in giorni, mesi e anni con jQuery

Piuttosto che mostrare solo il numero di giorni che voglio calcolare in giorni & mesi o giorni, mesi e anni a seconda del numero. Ho una dichiarazione if per la scissione ma non riesco a capire come funziona la matematica per passare da 132 giorni a x giorni x mesi ... Qualche suggerimento?

// 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'); 
} 
+2

I mesi hanno un numero diverso di giorni. C'è qualche data di inizio o vuoi semplicemente assumere un mese di 30 giorni? –

+0

Yeap può assumere 30 per questa ... risposta qui sotto funziona alla grande! – tbwcf

risposta

11

Provare qualcosa del genere;

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; 
} 

Si prevede che l'argomento sia la differenza in giorni che si desidera rappresentare. Si presuppone un mese di 30 giorni e un anno di 365.

Si dovrebbe usare in questo modo;

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

http://jsfiddle.net/0zgr5gfj/

5

un tentativo da me (quest'anno prendere salto in considerazione e in base alla data corrente)

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)); 
1

Si tratta di una soluzione più semplice che ho fatto senza il per 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/

Problemi correlati