private string GetAge(DateTime birthday) { DateTime Now = DateTime.Now; //Get Years int Years = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year - 1; //Get Past Year to calculate months, days and time DateTime dtPastYearDate = birthday.AddYears(Years); int Months = 0; for (int i = 1; i <= 12; i++) { if (dtPastYearDate.AddMonths(i) == Now) { Months = i; break; } else if (dtPastYearDate.AddMonths(i) >= Now) { Months = i - 1; break; } } int Days = Now.Subtract(dtPastYearDate.AddMonths(Months)).Days; int Hours = Now.Subtract(dtPastYearDate).Hours; int Minutes = Now.Subtract(dtPastYearDate).Minutes; int Seconds = Now.Subtract(dtPastYearDate).Seconds; return String.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s) {3} Hour(s) {4} Second(s)", Years, Months, Days, Hours, Seconds); }
Thursday, July 4, 2013
Age Calculation in C#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment