C# Formatting Age - Regarding Days, Weeks , Months - Years

后端 未结 4 1617
心在旅途
心在旅途 2021-01-18 02:20

I am working on some medical software and I am required to output all ages in a very specific manner, based on the following rules:

 If under 6 Weeks old :           


        
4条回答
  •  悲&欢浪女
    2021-01-18 03:04

    You can get an object representing the user's current age with a simple subtraction:

    TimeSpan age = DateTime.Now - dateOfBirth;
    

    And then it's just a matter of doing a bunch of 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 your formatting.

提交回复
热议问题