403 Stimmen

Berechnung des Alters anhand des Geburtsdatums im Format JJJJMMTT

Wie kann ich ein Alter in Jahren berechnen, wenn ich ein Geburtsdatum im Format JJJJMMTT habe? Ist es möglich mit Hilfe der Date() Funktion?

Ich bin auf der Suche nach einer besseren Lösung als der, die ich jetzt benutze:

var dob = '19800810';
var year = Number(dob.substr(0, 4));
var month = Number(dob.substr(4, 2)) - 1;
var day = Number(dob.substr(6, 2));
var today = new Date();
var age = today.getFullYear() - year;
if (today.getMonth() < month || (today.getMonth() == month && today.getDate() < day)) {
  age--;
}
alert(age);

14voto

Kinergy Punkte 305

Hier ist meine Lösung, geben Sie einfach in einem parseable Datum:

function getAge(birth) {
  ageMS = Date.parse(Date()) - Date.parse(birth);
  age = new Date();
  age.setTime(ageMS);
  ageYear = age.getFullYear() - 1970;

  return ageYear;

  // ageMonth = age.getMonth(); // Accurate calculation of the month part of the age
  // ageDay = age.getDate();    // Approximate calculation of the day part of the age
}

12voto

Josh Punkte 706

Eine alternative Lösung, denn warum nicht?

function calculateAgeInYears (date) {
    var now = new Date();
    var current_year = now.getFullYear();
    var year_diff = current_year - date.getFullYear();
    var birthday_this_year = new Date(current_year, date.getMonth(), date.getDate());
    var has_had_birthday_this_year = (now >= birthday_this_year);

    return has_had_birthday_this_year
        ? year_diff
        : year_diff - 1;
}

8voto

cr0ybot Punkte 3835

Diese Frage ist über 10 Jahre alt und niemand hat sich mit der Aufforderung befasst, dass das Geburtsdatum bereits im Format JJJJMMTT vorliegt?

Wenn Sie ein vergangenes Datum und das aktuelle Datum im Format JJJJMMTT haben, können Sie sehr schnell die Anzahl der dazwischen liegenden Jahre wie folgt berechnen:

var pastDate = '20101030';
var currentDate = '20210622';
var years = Math.floor( ( currentDate - pastDate ) * 0.0001 );
// 10 (10.9592)

Sie können das aktuelle Datum formatiert erhalten als YYYYMMDD wie diese:

var now = new Date();

var currentDate = [
    now.getFullYear(),
    ('0' + (now.getMonth() + 1) ).slice(-2),
    ('0' + now.getDate() ).slice(-2),
].join('');

6voto

Sarwar Punkte 61
function age()
{
    var birthdate = $j('#birthDate').val(); // in   "mm/dd/yyyy" format
    var senddate = $j('#expireDate').val(); // in   "mm/dd/yyyy" format
    var x = birthdate.split("/");    
    var y = senddate.split("/");
    var bdays = x[1];
    var bmonths = x[0];
    var byear = x[2];
    //alert(bdays);
    var sdays = y[1];
    var smonths = y[0];
    var syear = y[2];
    //alert(sdays);

    if(sdays < bdays)
    {
        sdays = parseInt(sdays) + 30;
        smonths = parseInt(smonths) - 1;
        //alert(sdays);
        var fdays = sdays - bdays;
        //alert(fdays);
    }
    else{
        var fdays = sdays - bdays;
    }

    if(smonths < bmonths)
    {
        smonths = parseInt(smonths) + 12;
        syear = syear - 1;
        var fmonths = smonths - bmonths;
    }
    else
    {
        var fmonths = smonths - bmonths;
    }

    var fyear = syear - byear;
    document.getElementById('patientAge').value = fyear+' years '+fmonths+' months '+fdays+' days';
}

6voto

romuleald Punkte 1348

Ich denke, das könnte einfach so sein:

function age(dateString){
    let birth = new Date(dateString);
    let now = new Date();
    let beforeBirth = ((() => {birth.setDate(now.getDate());birth.setMonth(now.getMonth()); return birth.getTime()})() < birth.getTime()) ? 0 : 1;
    return now.getFullYear() - birth.getFullYear() - beforeBirth;
}

age('09/20/1981');
//35

Funktioniert auch mit einem Zeitstempel

age(403501000000)
//34

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X