C # Formatting eyelids - regarding days, weeks, months - years

I am working on some medical software, and I need to derive all ages in a very specific way based on the following rules:

If under 6 Weeks old : ###D (Number of Days) If under 6 Months old : ###W (Number of Weeks) If under 2 Years old : ###M (Number of Months) If above 2 Years old : ###Y (Number of Years) 

Using C # I'm trying to find an easy way to do this, simply using Person's Date of Birth, any help would be greatly appreciated.

+7
source share
4 answers

Yesterday I was working on something similar, but something like this should fit your needs: (within 7 days, 31 days, 365 days, etc.)

Revised Method: (corrected according to Bob's suggestions)

 public static string ConvertAge(DateTime dob) { DateTime today = DateTime.Today; string fmt = "{0:0##}{1}"; //Greater than 2 Years old - Ouput Years if (dob <= today.AddYears(-2)) return string.Format(fmt, (dob.DayOfYear <= today.DayOfYear) ? (today.Year - dob.Year) : (today.Year - dob.Year)-1, "Y"); //Less than 2 Years - Output Months if (dob < today.AddMonths(-2)) return string.Format(fmt, (dob.DayOfYear <= today.DayOfYear) ? (today.Year - dob.Year) * 12 + (today.Month - dob.Month) : ((today.Year - dob.Year) * 12 + (today.Month - dob.Month))-1 , "M"); //Less than 2 Months - Output Weeks if (dob < today.AddDays(-2 * 7)) return string.Format(fmt, (today - dob).Days / 7, "W"); //Less than 2 Weeks - Output Days return string.Format(fmt, (today - dob).Days, "D"); } 

Previous Method:

 public string ConvertAge(DateTime dateOfBirth) { int daysOld = (DateTime.Now - dateOfBirth).Days; //Age < 6 Weeks if (daysOld < (6 * 7)) return String.Format("{0:0##}{1}", daysOld, 'D'); //Age < 6 Months else if (daysOld < (6 * 31)) return String.Format("{0:0##}{1}", daysOld/7, 'W'); //Age < 2 Years else if (daysOld < (2 * 365)) return String.Format("{0:0##}{1}", daysOld / 31, 'M'); //Age >= 2 Years else return String.Format("{0:0##}{1}", daysOld / 365, 'Y'); } 

Hope this helps!

+7
source

The DateTime type can be subtracted from other DateTimes, resulting in a TimeSpan representing a space. Try the following:

 var timeAlive = DateTime.Today - dateOfBirth.Date; 

Then view the days, months, and years (divide the days by 7 for weeks) based on time and adjust accordingly.

+3
source

No assumptions about days / months or a year are made below.
On the other hand, it is not compatible with Y3K.

  public static string GetAge (DateTime dob) { DateTime today = DateTime.Now; string fmt = "{0:0##}{1}"; if (dob < today.AddYears(-2)) return string.Format(fmt, today.Year - dob.Year, "Y"); if (dob < today.AddMonths(-6))return string.Format(fmt, (today.Year - dob.Year)*12 + (today.Month - dob.Month), "M"); if (dob < today.AddDays(-6 * 7)) return string.Format(fmt, (today - dob).Days/7, "W"); return string.Format(fmt, (today - dob).Days, "D"); } 
+2
source

You can get an object representing the current age of the user, with a simple subtraction:

 TimeSpan age = DateTime.Now - dateOfBirth; 

And then it's just a matter of creating a heap if clauses

 if (age.TotalDays < 6 * 7) // 6 weeks // ... else if (age.TotalDays < 6 * 30) // 6 months // ... // et cetera 

You should be able to figure out how to do formatting.

0
source

All Articles