cuando la fecha de nacimiento de menos de 1 año muestra 0 años, quiero convertirlo a meses y días

In my project I am working on patient flow where if the patient only a couple days old then my UI is just showing 0 year old instead of, for example 2 days old or 6 months old.

I want to include this logic to calculate age in months and days of patient.

abajo esta el C# function i am using for calculating age of patient:

public int CalculatedAge
{
    get
    {
        if (Patient.DateOfBirth == null && !Patient.Age.HasValue)
            return 0;
        else if (Patient.DateOfBirth == null && Patient.Age != null && Patient.Age.HasValue)
            return Patient.Age.Value;
        else if (Patient.DateOfBirth != null)
            return DateTime.Now.ToUniversalTime().Year - Patient.DateOfBirth.Value.Year;
        return 0;
    }
}

preguntado el 28 de mayo de 14 a las 12:05

You can't fit that in an int. -

Do you really need all those null checks there? Might be better if you assume (and possibly assert, if you really want instead of just letting null pointer exception happen) that they are not null. You'll certainly end up with much cleaner code that way. -

Return a TimeSpan to represent the Age, not an int. -

You should also rethink you age calculation logic. Just subtracting years will give incorrect results (before and after the birthdate). Storing both Birthdate and Age is redundant and gives room for errors. -

@karim TimeSpan struggles with months -

2 Respuestas

You can create new entities.

enum AgeUnits
{
    Days,
    Months,
    Years
}

class Age
{
    public int Value { get; private set; }
    public AgeUnits Units { get; private set; }

    public Age(int value, AgeUnits ageUnits)
    {
        Value = value;
        Units = ageUnits;
    }
}

Entonces puedes usar Age como tipo de CalculatedAge propiedad.

public Age CalculatedAge
{
    get
    {
        if (Patient.DateOfBirth.HasValue)
        {
            DateTime bday = Patient.DateOfBirth.Value;
            DateTime now = DateTime.UtcNow;
            if (bday.AddYears(1) < now)
            {
                int years = now.Year - bday.year;
                if (bday > now.AddYears(-years))
                    years--;
                return new Age(years, AgeUnits.Years);
            }
            else if (bday.AddMonths(1) < now)
            {
                int months = (now.Months - bday.Months + 12) % 12;
                if (bday > now.AddMonths(-months))
                    months--;
                return new Age(months, AgeUnits.Months);
            }
            else
            {
                int days = (now - bday).Days;
                return new Age(days, AgeUnits.Days);
            }
        }
        else
        {
            if (Patient.Age.HasValue)
                return new Age(Patient.Age.Value, AgeUnits.Years);
            else
                return null;
        }
    }
}

contestado el 28 de mayo de 14 a las 13:05

public Age CalculatedAge
{
    get
    {
        if (Patient.DateOfBirth == null && Patient.Age != null && Patient.Age.HasValue)
            return new Age(Patient.Age.Value);
        else if (Patient.DateOfBirth != null)
        {
            DateTime now = DateTime.UtcNow;
            TimeSpan tsAge = now - Patient.DateOfBirth;
            DateTime age = new DateTime(tsAge.Ticks);
            return new Age(age.Year - 1, age.Month - 1, age.Day - 1);
        }
        return new Age(0);
    }
}

Y este es el Age estructura:

struct Age
{
    public int Years, Months, Days; //It's a small struct, properties aren't necessary
    public Age(int years, int months = 0, int days = 0) { this.Years = years; this.Months = months; this.Days = days; }
}

Obviously you can just check the values in with IF statements but a struct for this purpose is cleaner in my opinion. return new Age(age.Year - 1, age.Month - 1, age.Day - 1); This part is due to the fact that the MinValue of a DateTime object is 01/01/0001 so you need to substract that to get the real age.

contestado el 28 de mayo de 14 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.