c# Function CalculateYourAge
- public static string CalculateYourAge(DateTime Dob)
- {
- DateTime Now = DateTime.Now;
- int _Years = new DateTime(DateTime.Now.Subtract(Dob).Ticks).Year - 1;
- DateTime _DOBDateNow = Dob.AddYears(_Years);
- int _Months = 0;
- for (int i = 1; i <= 12; i++)
- {
- if (_DOBDateNow.AddMonths(i) == Now)
- {
- _Months = i;
- break;
- }
- else if (_DOBDateNow.AddMonths(i) >= Now)
- {
- _Months = i - 1;
- break;
- }
- }
- int Days = Now.Subtract(_DOBDateNow.AddMonths(_Months)).Days;
-
- return $"Age is {_Years} Years {_Months} Months {Days} Days";
- }
No comments:
Post a Comment