C # හි, ඩේටයිම් වර්ගයේ උපන් දිනය මත පදනම්ව යමෙකුගේ වයස ගණනය කරන්නේ කෙසේද?


1880

දී ඇති DateTimeපුද්ගලයෙකු උපන් නියෝජනය, කොහොමද මම අවුරුදු ඔවුන්ගේ වයස ගණනය කරන්නේ කෙසේද?


147
මෙතෙක් ලබා දී ඇති සියලුම පිළිතුරු වලට අනුව එය රඳා පවතින්නේ පුද්ගලයා ඉපදුණේ කොතැනද සහ ඔවුන් දැන් සිටින්නේ කොහේද යන්නයි.
යෝර්

40
Aaaur: දැන් + උපන් කාලය GMT / UTC බවට පරිවර්තනය කරන්න, වයස යනු සාපේක්ෂ අගයක් පමණි, එබැවින් කාල කලාප අදාළ නොවේ. පරිශීලකයාගේ වර්තමාන කාල කලාපය තීරණය කිරීම සඳහා, ඔබට භූ-ස්ථානගත කිරීම භාවිතා කළ හැකිය.
ස්ටෙෆාන් ස්ටීගර්

[ජූලියන් දිනය] [1] සලකා නොබලන්නේ ඇයි? [1]: stackoverflow.com/questions/7103064/…
මුහම්මද් හෙවිඩි

5
හරස් කාල කලාප ගණනය කිරීම් පිළිබඳ ඔබේ යෝජනාව අපි සැලකිල්ලට ගන්නේ නම්, දිවා ආලෝකය ඉතිරි කිරීමේ කාලය ගණනය කිරීමට කිසියම් ආකාරයකින් බලපාන්නේද?
ඩීඩීඑම්

6
පහත් කොට සලකනු ලැබුවේ මෙය පැහැදිලිවම ගෙදර වැඩ ප්‍රශ්නයක් වන අතර දැනට පවතින උත්සාහයක් ලබා දී නොමැති බැවිනි.
මාරි

Answers:


2139

තේරුම් ගැනීමට පහසු සහ සරල විසඳුමක්.


// Save today's date.
var today = DateTime.Today;

// Calculate the age.
var age = today.Year - birthdate.Year;

// Go back to the year the person was born in case of a leap year
if (birthdate.Date > today.AddYears(-age)) age--;

කෙසේ වෙතත්, මෙය උපකල්පනය කරන්නේ ඔබ වයස පිළිබඳ බටහිර අදහස සොයන අතර නැගෙනහිර ආසියානු ගණනය කිරීම් භාවිතා නොකරන බවයි.


252
DateTime.Now කාර්යසාධනය පිළිබඳව අදහස් දැක්වීමට අවශ්‍ය විය. ඔබට නිවැරදි කාල කලාප අගයක් අවශ්‍ය නොවේ නම්, DateTime භාවිතා කරන්න. දැන් එය වඩා වේගවත්ය.
JAG

104
අපි උපන් දින කතා කරන විට ඔබට ඩේටයිම් භාවිතා කළ හැකිය. අද දින ලබා දී ඇති කාලයට අදාළත්වයක් නැත.
ට්‍රිස්ටන් වෝනර්-ස්මිත්

78
මෙම පිළිතුර සියලුම ස්ථාන සහ සියලුම වයස් කාණ්ඩ සමඟ ක්‍රියා නොකරයි. රුසියාව (1918), ග්‍රීසිය (1924) සහ තුර්කිය (1926) ඇතුළු වර්තමාන ජීවත්වන පුද්ගලයින්ගේ උපතින් පසු රටවල් කිහිපයක් දින මඟ හැර ඇත.
Lars D

30
ඇත්තටම එය තවමත් සම්පූර්ණයෙන්ම නිවැරදි නැත. මෙම කේතය අනුමාන කරන්නේ 'bday' යනු DateTime හි දිනය-කොටස බවයි. එය අද්දර නඩුවකි (මම අනුමාන කරන්නේ බොහෝ අය දින වකවානු මිස දින වකවානු නොවේ), නමුත් ඔබ උපන්දිනයක දිනයක් හා වේලාවක් ලෙස ගත කරන්නේ නම් වේලාව 00:00:00 ට වඩා වැඩි නම් ඔබ ' ඩැන්විල් පෙන්වා දුන් දෝෂයට මම දිව යන්නෙමි. Bday = bday.Date සැකසීම මෙය නිවැරදි කරයි.
අයිවින්ඩ්

121
අන්තිම පේළිය මට ඕනෑවට වඩා සිතීමට සැලැස්වීය. ඒ වෙනුවට කෙසේද: if (bday.AddYears (වයස)> දැන්) වයස--; මෙය වඩාත් බුද්ධිමත් ප්‍රකාශනයක් ලෙස පෙනේ.
cdiggins

1018

මෙය සිදු කිරීම සඳහා අමුතුම ක්‍රමයකි, නමුත් ඔබ yyyymmddඋපන්දිනය වර්තමාන දිනයෙන් අඩු කර අඩු කළහොත් ඔබට වයස ඇති අවසාන ඉලක්කම් 4 අතහරින්න :)

මම C # දන්නේ නැත, නමුත් මෙය ඕනෑම භාෂාවකින් ක්‍රියාත්මක වනු ඇතැයි මම විශ්වාස කරමි.

20080814 - 19800703 = 280111 

අවසාන ඉලක්කම් 4 අතහරින්න = 28.

සී # කේතය:

int now = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
int dob = int.Parse(dateOfBirth.ToString("yyyyMMdd"));
int age = (now - dob) / 10000;

හෝ විකල්පයක් ලෙස සියලු වර්ගවල පරිවර්තනයකින් තොරව ව්‍යාප්ති ක්‍රමයක් ලෙස. දෝෂ පරීක්ෂා කිරීම මඟ හැරී ඇත:

public static Int32 GetAge(this DateTime dateOfBirth)
{
    var today = DateTime.Today;

    var a = (today.Year * 100 + today.Month) * 100 + today.Day;
    var b = (dateOfBirth.Year * 100 + dateOfBirth.Month) * 100 + dateOfBirth.Day;

    return (a - b) / 10000;
}

6
ඇත්ත වශයෙන්ම මෙය MS-SQL හි දිවා කාලයේ ක්ෂේත්‍ර සමඟ භාවිතා කිරීම සඳහා විශිෂ්ටයි (01-011900 සිට මුළු දින)
පැට්‍රික්

5
umenumerek කරුණාකර ඔබ යෝජනා කළ වෙනස් කිරීම් ඔවුන්ගේම පිළිතුරක් ලෙස පළ කරන්න. එය වටින දෙය නම්, වර්තමාන වර්ෂ 10000 විශාලත්වයේ අනුපිළිවෙලවල් දෙකකින් පූර්ණ සංඛ්‍යා පිටාර ගැලීමකට ආසන්නව නැත. 20,150,000 එදිරිව 2,147,483,648
ගැලැක්ටික්

7
-ලෝන්චාල්ක් 20180101 - 20171231 = 8870. අවසාන ඉලක්කම් 4 අතහරින්න, ඔබට 0වයස සඳහා (ඇඟවුම් කර ඇත) . ඔබට ලැබුණේ 1කෙසේද?
රූෆස් එල්

4
මෙය පැරණි පිළිතුරක් බව මම දනිමි, නමුත් මම එයින් දිගු කිරීමේ ක්‍රමයක් නොකරමි, එවැනි තර්කනයන් අර්ථ දැක්වීමට සුදුසුම ස්ථානය එය නොවේ.
ලූකා ෆෙරී

2
මෙය මොන වගේ මන්ත්‍ර ගුරුකම්ද?
Muleskinner

391

මෙන්න පරීක්ෂණ ස්නිපටයක්:

DateTime bDay = new DateTime(2000, 2, 29);
DateTime now = new DateTime(2009, 2, 28);
MessageBox.Show(string.Format("Test {0} {1} {2}",
                CalculateAgeWrong1(bDay, now),      // outputs 9
                CalculateAgeWrong2(bDay, now),      // outputs 9
                CalculateAgeCorrect(bDay, now),     // outputs 8
                CalculateAgeCorrect2(bDay, now)));  // outputs 8

මෙන්න ඔබට ක්රම තිබේ:

public int CalculateAgeWrong1(DateTime birthDate, DateTime now)
{
    return new DateTime(now.Subtract(birthDate).Ticks).Year - 1;
}

public int CalculateAgeWrong2(DateTime birthDate, DateTime now)
{
    int age = now.Year - birthDate.Year;

    if (now < birthDate.AddYears(age))
        age--;

    return age;
}

public int CalculateAgeCorrect(DateTime birthDate, DateTime now)
{
    int age = now.Year - birthDate.Year;

    if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
        age--;

    return age;
}

public int CalculateAgeCorrect2(DateTime birthDate, DateTime now)
{
    int age = now.Year - birthDate.Year;

    // For leap years we need this
    if (birthDate > now.AddYears(-age)) 
        age--;
    // Don't use:
    // if (birthDate.AddYears(age) > now) 
    //     age--;

    return age;
}

33
මෙම කේතය ක්‍රියාත්මක වන අතර, එය අවධාරනය කරන්නේ අධික දිනයක උපත ලැබූ පුද්ගලයෙකු පෙබරවාරි 28 වන දිනට වඩා මාර්තු 1 වන දින පිම්ම නොවන වසරවල ඊළඟ වයස කරා ළඟා වන බවයි. යථාර්ථයේ දී, එක්කෝ විකල්පය නිවැරදි විය හැකිය . විකිපීඩියාවට මේ ගැන යමක් කීමට ඇත . එබැවින් ඔබේ කේතය "වැරදි" නොවන අතර පිළිගත් විසඳුම ද නොවේ.
මැට් ජොන්සන්-පින්ට්

19
Att මැට් ජොන්සන් මම හිතන්නේ එය ඇත්ත වශයෙන්ම නිවැරදි ය. මගේ දිනය පෙබරවාරි 29 නම්, පෙබරවාරි 28 මගේ දිනය සමත්ව නැත, මම තවමත් පෙබරවාරි 27 වන දිනට සමාන විය යුතුය. කෙසේ වෙතත්, මාර්තු 1 වන දින, අපි මගේ දිනය පසු කර ඇති අතර මම ඊළඟ වයස විය යුතුය. එක්සත් ජනපදයේ, මත්පැන් අලෙවි කරන ව්‍යාපාරයකට “ඔබ මේ දිනට පසු YYYY හි උපත ලැබුවා නම්, ඔබට මත්පැන් මිලදී ගත නොහැක” (සෑම වසරකම YYYY වෙනස් වන තැන) වැනි ලකුණක් ඇත. එයින් අදහස් වන්නේ පෙබරවාරි 29 වන දින උපත ලැබූ කෙනෙකුට 21 වන වියට පා තබන වසරේ (බොහෝ ස්ථාන) පෙබරවාරි 28 වන දිනට මත්පැන් මිලදී ගත නොහැකි බවත්, ඔවුන් මාර්තු 1 වනදා දක්වා වයසින් වැඩි නොවන බවට වන අදහස සඳහා සහාය ලබා දෙන
බවත්ය.

4
@ jfren484 - විකිපීඩියා ලිපිය කියවන්න. අධිකරණ බලය හරහා එය සැලකිය යුතු ලෙස වෙනස් වේ.
මැට් ජොන්සන්-පින්ට්

9
claim jfren484 ඔබගේ ප්‍රකාශයට දර්ශනයට කිසිම සම්බන්ධයක් නැත; නමුත් සෑම දෙයක්ම ඔබගේ පෞද්ගලික හැඟීම සමඟ කළ යුතුය. පෙබරවාරි 29 වන දින උපත ලැබූ පුද්ගලයෙකුට වයස 'නීතිමය වයස් සීමාවක්' නොවන්නේ නම් බොහෝ විට වැදගත් නොවේ (උදා: මත්පැන් මිලදී ගැනීමට, ඡන්දය දීමට, විශ්‍රාම වැටුප ලබා ගැනීමට, හමුදාවට බැඳීමට, රියදුරු බලපත්‍ර ලබා ගැනීමට). එක්සත් ජනපදයේ පානීය වයස (අවුරුදු 21) සලකා බලන්න: බොහෝ මිනිසුන් සඳහා එය දින 7670 කි. අධික වර්ෂයකදී පෙබරවාරි 29 ට පෙර හෝ අධික වර්ෂයට පෙර මාර්තු 1 සිට උපත ලැබුවහොත් එය දින 7671 කි. පෙබරවාරි 29 වන දින උපත ලැබුවහොත්: 28 පෙබරවාරි දින 7670 ක් සහ 1 මාර්තු දින 7671 කි. තේරීම අත්තනෝමතික බැවින් එය දෙපැත්තටම යා හැකිය.
කලකිරුණු

4
RaCraigYoung දාර්ශනිකව මා අදහස් කළේ කුමක්දැයි ඔබට තේරෙන්නේ නැත. මම එම පදය නීත්‍යානුකූලව වෙනස් ලෙස භාවිතා කළෙමි. යමෙකු පුද්ගලයෙකුගේ නෛතික වයස දැන ගැනීමට අවශ්‍ය අයදුම්පතක් ලියන්නේ නම්, ඔවුන් දැනගත යුතු වන්නේ පෙබරවාරි 29 වන දින උපත ලද පුද්ගලයින්ට ප්‍රතිකාර කිරීම සඳහා / භාවිතා කිරීම සඳහා ඔවුන්ගේ අයදුම්පත භාවිතා කරන නෛතික අධිකරණ බලය පමණි. කෙසේ වෙතත්, අපි එයට සැලකිය යුතු ආකාරය ගැන කතා කිරීම , එවිට එය අර්ථ දැක්වීම අනුව දර්ශනයයි. ඔව්, මා දුන් මතය මගේම මතයකි, නමුත් මා පැවසූ පරිදි, පෙබරවාරි 28 වන දිනට වඩා මාර්තු 1 වනදා තර්ක කිරීම පහසු වනු ඇතැයි මම සිතමි.
jfren484

110

මේ සඳහා ඇති සරල පිළිතුර නම් AddYearsපහත දැක්වෙන පරිදි අයදුම් කිරීමයි . මන්දයත් අධික වර්ෂවල පෙබරවාරි 29 වනදාට වසර එකතු කර පොදු වසර සඳහා පෙබරවාරි 28 වන දින නිවැරදි ප්‍රති result ල ලබා ගත හැකි එකම දේශීය ක්‍රමය මෙය වන බැවිනි.

සමහරු සිතන්නේ මාර්තු 1 වන දින පිම්මගේ උපන් දිනය බවයි .නෙට් හෝ කිසිදු නිල රීතියක් මෙයට සහාය නොදක්වන අතර පෙබරවාරි මාසයේ උපත ලද සමහරුන්ට ඔවුන්ගේ උපන්දිනයෙන් 75% ක් තවත් මාසයක් තුළ තිබිය යුත්තේ මන්දැයි පොදු තර්කනය පැහැදිලි නොකරයි.

තවද, වයස් ක්‍රමයක් මඟින් දිගුවක් ලෙස එකතු කිරීමට ඉඩ ලබා දේ DateTime. මෙයින් ඔබට හැකි සරලම ආකාරයෙන් වයස ලබා ගත හැකිය:

  1. ලැයිස්තු අයිතමය

int age = birthDate.Age ();

public static class DateTimeExtensions
{
    /// <summary>
    /// Calculates the age in years of the current System.DateTime object today.
    /// </summary>
    /// <param name="birthDate">The date of birth</param>
    /// <returns>Age in years today. 0 is returned for a future date of birth.</returns>
    public static int Age(this DateTime birthDate)
    {
        return Age(birthDate, DateTime.Today);
    }

    /// <summary>
    /// Calculates the age in years of the current System.DateTime object on a later date.
    /// </summary>
    /// <param name="birthDate">The date of birth</param>
    /// <param name="laterDate">The date on which to calculate the age.</param>
    /// <returns>Age in years on a later day. 0 is returned as minimum.</returns>
    public static int Age(this DateTime birthDate, DateTime laterDate)
    {
        int age;
        age = laterDate.Year - birthDate.Year;

        if (age > 0)
        {
            age -= Convert.ToInt32(laterDate.Date < birthDate.Date.AddYears(age));
        }
        else
        {
            age = 0;
        }

        return age;
    }
}

දැන්, මෙම පරීක්ෂණය ක්‍රියාත්මක කරන්න:

class Program
{
    static void Main(string[] args)
    {
        RunTest();
    }

    private static void RunTest()
    {
        DateTime birthDate = new DateTime(2000, 2, 28);
        DateTime laterDate = new DateTime(2011, 2, 27);
        string iso = "yyyy-MM-dd";

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.WriteLine("Birth date: " + birthDate.AddDays(i).ToString(iso) + "  Later date: " + laterDate.AddDays(j).ToString(iso) + "  Age: " + birthDate.AddDays(i).Age(laterDate.AddDays(j)).ToString());
            }
        }

        Console.ReadKey();
    }
}

තීරණාත්මක දිනය උදාහරණය මෙයයි:

උපන් දිනය: 2000-02-29 පසු දිනය: 2011-02-28 වයස: 11

ප්‍රතිදානය:

{
    Birth date: 2000-02-28  Later date: 2011-02-27  Age: 10
    Birth date: 2000-02-28  Later date: 2011-02-28  Age: 11
    Birth date: 2000-02-28  Later date: 2011-03-01  Age: 11
    Birth date: 2000-02-29  Later date: 2011-02-27  Age: 10
    Birth date: 2000-02-29  Later date: 2011-02-28  Age: 11
    Birth date: 2000-02-29  Later date: 2011-03-01  Age: 11
    Birth date: 2000-03-01  Later date: 2011-02-27  Age: 10
    Birth date: 2000-03-01  Later date: 2011-02-28  Age: 10
    Birth date: 2000-03-01  Later date: 2011-03-01  Age: 11
}

සහ පසු දිනය සඳහා 2012-02-28:

{
    Birth date: 2000-02-28  Later date: 2012-02-28  Age: 12
    Birth date: 2000-02-28  Later date: 2012-02-29  Age: 12
    Birth date: 2000-02-28  Later date: 2012-03-01  Age: 12
    Birth date: 2000-02-29  Later date: 2012-02-28  Age: 11
    Birth date: 2000-02-29  Later date: 2012-02-29  Age: 12
    Birth date: 2000-02-29  Later date: 2012-03-01  Age: 12
    Birth date: 2000-03-01  Later date: 2012-02-28  Age: 11
    Birth date: 2000-03-01  Later date: 2012-02-29  Age: 11
    Birth date: 2000-03-01  Later date: 2012-03-01  Age: 12
}

4
තාක්‍ෂණිකව, පෙබරවාරි 29 වන දින උපන්දිනය මාර්තු 1 වන දිනට යෙදීම පිළිබඳ අදහස් දැක්වීමක් 28 වන දිනට වඩා කල් වැඩියි (ඇත්ත වශයෙන්ම දින 1 ක් කලින්). 1 වන දින එක් දිනක් ප්‍රමාද වැඩියි. නමුත් උපන්දිනය අතර බැවින්, පිම්ම නොවන අවුරුදු වල වයස ගණනය කිරීම සඳහා 1 වන ක්‍රමය භාවිතා කිරීම මට වඩාත් අර්ථවත් කරයි, මන්ද එම පුද්ගලයා සැබවින්ම සෑම වසරකම මාර්තු 1 වන දින (සහ 2 වන සහ 3 වන) වයස්ගතව සිටින නමුත් පෙබරවාරි 28 වන දින නොවේ.
සයිබර් ක්ලා

1
මෘදුකාංග සැලසුම් ස්ථානයක සිට, මෙය දිගු කිරීමේ ක්‍රමයක් ලෙස ලිවීම මට එතරම් තේරුමක් නැත. date.Age(other)?
අඟහරු

90

මගේ යෝජනාව

int age = (int) ((DateTime.Now - bday).TotalDays/365.242199);

වර්ෂය නියම දිනය අනුව වෙනස් වන බව පෙනේ. (වයස අවුරුදු 107 දක්වා පරීක්ෂා කර ඇති බව මට පෙනේ.)


26
හැරී පැච් ඔබේ ස්ථාන පරීක්ෂා කිරීමේ ක්‍රමවේදය අගය කරනු ඇතැයි මම නොසිතමි: latimes.com/news/obituaries/…
MusiGenesis

3
ගූගල් පවසයිdays in a year = 365.242199
mpen

12
ග්‍රෙගෝරියන් දින දර්ශනයේ වසරක සාමාන්‍ය දිග දින 365.2425 කි.
dan04

4
මම කියන්නේ, මෙය සරලම විසඳුම්වලින් එකක් වන අතර එය ප්‍රමාණවත්ය . මගේ X වන උපන්දිනයට දින භාගයකට පෙර සහ වැඩසටහන පවසන්නේ මට වයස අවුරුදු X ක් නම් කවුද සැලකිලිමත් වන්නේ. ගණිතමය නොවුනත් වැඩසටහන වැඩි වශයෙන් හෝ නිවැරදි ය. මම ඇත්තටම මෙම විසඳුමට කැමතියි.
පීටර් පර්හා

13
^^ සමහර විට එය වැදගත් නිසා. මගේ පරීක්ෂණයේදී මෙය පුද්ගලයන්ගේ උපන්දිනය අසමත් වන අතර, එය ඔවුන්ට වඩා වයසින් අඩු බව වාර්තා කරයි.
චැඩ්ට්

76

තවත් කාර්යයක්, මා විසින් නොව වෙබයේ සොයාගෙන එය ටිකක් පිරිපහදු කළේය:

public static int GetAge(DateTime birthDate)
{
    DateTime n = DateTime.Now; // To avoid a race condition around midnight
    int age = n.Year - birthDate.Year;

    if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day))
        age--;

    return age;
}

මගේ මතකයට එන කරුණු දෙකක් පමණි: ග්‍රෙගෝරියන් දින දර්ශනය භාවිතා නොකරන රටවල පුද්ගලයින් ගැන කුමක් කිව හැකිද? DateTime.Now දැන් මා සිතන්නේ සේවාදායක විශේෂිත සංස්කෘතිය තුළ ය. ඇත්ත වශයෙන්ම ආසියානු දින දර්ශන සමඟ වැඩ කිරීම පිළිබඳ මට ශුන්‍ය දැනුමක් ඇති අතර දින දර්ශන අතර දින පරිවර්තනය කිරීමට පහසු ක්‍රමයක් තිබේදැයි මම නොදනිමි, නමුත් 4660 වර්ෂයේ සිට ඔබ එම චීන ජාතිකයන් ගැන කල්පනා කරන්නේ නම් :-)


මෙය විවිධ කලාප (දින ආකෘති) හොඳම ලෙස හැසිරවීමට පෙනේ.
webdad3

53

විසඳිය යුතු ප්‍රධාන ගැටළු:

1. නිවැරදි වයස ගණනය කරන්න - අවුරුදු, මාස, දින ආදිය.

2. සාමාන්‍යයෙන් වටහා ගත් වයස ගණනය කරන්න - මිනිසුන් සාමාන්‍යයෙන් ඔවුන්ගේ වයස හරියටම නොසලකයි, වත්මන් වසරේ ඔවුන්ගේ උපන්දිනය කවදාදැයි ඔවුන් සැලකිලිමත් වේ.


1 සඳහා විසඳුම පැහැදිලිය:

DateTime birth = DateTime.Parse("1.1.2000");
DateTime today = DateTime.Today;     //we usually don't care about birth time
TimeSpan age = today - birth;        //.NET FCL should guarantee this as precise
double ageInDays = age.TotalDays;    //total number of days ... also precise
double daysInYear = 365.2425;        //statistical value for 400 years
double ageInYears = ageInDays / daysInYear;  //can be shifted ... not so precise

2 සඳහා විසඳුම යනු සම්පූර්ණ වයස තීරණය කිරීමේදී එතරම් නිරවද්‍ය නොවන නමුත් මිනිසුන් විසින් නිරවද්‍ය යැයි සැලකේ. මිනිසුන් සාමාන්‍යයෙන් ඔවුන්ගේ වයස "අතින්" ගණනය කරන විට එය භාවිතා කරයි:

DateTime birth = DateTime.Parse("1.1.2000");
DateTime today = DateTime.Today;
int age = today.Year - birth.Year;    //people perceive their age in years

if (today.Month < birth.Month ||
   ((today.Month == birth.Month) && (today.Day < birth.Day)))
{
  age--;  //birthday in current year not yet reached, we are 1 year younger ;)
          //+ no birthday for 29.2. guys ... sorry, just wrong date for birth
}

2 ට සටහන් :.

  • මෙය මගේ ප්‍රියතම විසඳුමයි
  • අධික වර්ෂවල දින ගණන වෙනස් කරන බැවින් අපට DateTime.DayOfYear හෝ TimeSpans භාවිතා කළ නොහැක.
  • කියවීමේ හැකියාව සඳහා තවත් පේළි කිහිපයක් මා එහි තබා ඇත

තවත් එක් සටහනක් ... මම ඒ සඳහා ස්ථිතික අධි බර පැටවීමේ ක්‍රම 2 ක් නිර්මාණය කරමි, එකක් විශ්වීය භාවිතය සඳහා, දෙවනුව භාවිතයට හිතකාමී බව:

public static int GetAge(DateTime bithDay, DateTime today) 
{ 
  //chosen solution method body
}

public static int GetAge(DateTime birthDay) 
{ 
  return GetAge(birthDay, DateTime.Now);
}

50

මෙන්න එක් ලයිනර් එකක්:

int age = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year-1;

23
මේක කැඩිල. පරීක්‍ෂා කළ හැකි ය: පොදු ස්ථිතික int ගණනය කිරීම් Age (DateTime dateOfBirth, DateTime dateToCalulateAge) new නව දිනය ටයිම් (dateToCalculateAge.Subtract (dateOfBirth) .Ticks) ආපසු ලබා දෙන්න .අපේ - 1; } ... මම 1990-06-01 ඇතුළත් කරන විට වයස අවුරුදු 14 ලබා දෙන අතර ඔහුගේ 14 වන උපන්දිනයට පෙර (1990-05-31) වයස ගණනය කරන්න.
ක්ජෙන්සන්

44

අධික වසර සහ සියල්ල නිසා මා දන්නා හොඳම ක්‍රමය නම්:

DateTime birthDate = new DateTime(2000,3,1);
int age = (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.25D);

43

අපි මෙහි භාවිතා කරන අනුවාදය මෙයයි. එය ක්‍රියාත්මක වන අතර එය තරමක් සරල ය. එය ජෙෆ්ගේ අදහසමයි, නමුත් මම සිතන්නේ එය ටිකක් පැහැදිලි කිරීම නිසා එය අඩු කිරීම සඳහා වන තර්කනය වෙන් කරන බැවින් එය තේරුම් ගැනීමට ටිකක් පහසු වේ.

public static int GetAge(this DateTime dateOfBirth, DateTime dateAsAt)
{
    return dateAsAt.Year - dateOfBirth.Year - (dateOfBirth.DayOfYear < dateAsAt.DayOfYear ? 0 : 1);
}

එවැනි දෙයක් අපැහැදිලි යැයි ඔබ සිතන්නේ නම්, එය වඩාත් පැහැදිලි කිරීම සඳහා ඔබට ත්‍රිමාණ ක්‍රියාකරු පුළුල් කළ හැකිය.

නිසැකවම මෙය සිදු කරනුයේ දීර් extension කිරීමේ ක්‍රමයක් ලෙස ය DateTime, නමුත් පැහැදිලිවම ඔබට එම එක් කේත පේළියක් අල්ලාගෙන ඕනෑම තැනක තැබිය හැකිය. මෙහිදී අපට සම්පූර්ණ කිරීමේ හැකියාව සඳහා දිගු කිරීමේ ක්‍රමයේ තවත් අධි බරක් DateTime.Nowඇත.


6
මම හිතන්නේ මෙය එක් දිනකින් හරියටම ඕෆ් බර්ත් හෝ ඩේට්ඒසාට් අධික වර්ෂයකදී වැටෙන විට. 2003 මාර්තු 1 වන දින 2004 පෙබරවාරි 29 වන දින උපත ලැබූ පුද්ගලයෙකුගේ වයස සලකා බලන්න. මෙය නිවැරදි කිරීම සඳහා, ඔබ (මාසය, දිවා ඕෆ් මාසය) යුගලවල ශබ්දකෝෂ සංසන්දනයක් කළ යුතු අතර එය කොන්දේසි සහිතව භාවිතා කරන්න.
ඩග් මැක්ලීන්

1
එය ඔබගේ උපන්දිනය වන විට නියම වයස පෙන්වීමටද යන්නේ නැත.
dotjoe

34

මම මෙය භාවිතා කරමි:

public static class DateTimeExtensions
{
    public static int Age(this DateTime birthDate)
    {
        return Age(birthDate, DateTime.Now);
    }

    public static int Age(this DateTime birthDate, DateTime offsetDate)
    {
        int result=0;
        result = offsetDate.Year - birthDate.Year;

        if (offsetDate.DayOfYear < birthDate.DayOfYear)
        {
              result--;
        }

        return result;
    }
}

32

මෙය මෙම ප්‍රශ්නයට "වැඩි විස්තර" ලබා දෙයි. සමහර විට ඔබ සොයන්නේ මෙයයි

DateTime birth = new DateTime(1974, 8, 29);
DateTime today = DateTime.Now;
TimeSpan span = today - birth;
DateTime age = DateTime.MinValue + span;

// Make adjustment due to MinValue equalling 1/1/1
int years = age.Year - 1;
int months = age.Month - 1;
int days = age.Day - 1;

// Print out not only how many years old they are but give months and days as well
Console.Write("{0} years, {1} months, {2} days", years, months, days);

1
මෙය සැමවිටම ක්‍රියාත්මක නොවේ. DateTime.MinValue සඳහා පරතරයක් එකතු කිරීමෙන් මෙය අධික වසර ගණනක් සඳහා ගණන් නොගනී. ඔබ අවුරුදු, මාස සහ දින වයස් වලට එකතු කළහොත් AddYears (), AddMonths සහ AddDays () ශ්‍රිතය භාවිතා කර එය සැමවිටම දිනය වේලාව ලබා නොදෙනු ඇත. .දැන් දිනය.
අතනාසියෝස් කතරස්

3
කාල පරාසය ස්වයංක්‍රීයව දින 2 ක් අතර වේගයෙන් වසර ගණන් ගනී, එබැවින් ඔබ ලබා ගන්නේ කුමක්දැයි මට විශ්වාස නැත. මම මයික්‍රොසොෆ්ට් සංසදවලින් විමසා ඇති අතර මයික්‍රොසොෆ්ට් විසින් දින 2 ක් අතර අධික වසරක් ගතවන බව සනාථ කර ඇත.
ජැකලින් ලොරියෝල්ට්

2
පහත දැක්වෙන TWO සෙනරියෝස් සලකා බලන්න. 1 වන දිනය වේලාව.දැන් 1/1/2001 වන අතර දරුවෙකු 1/1/2000 දින උපත ලබයි. 2000 යනු අධික වසරක් වන අතර එහි ප්‍රති result ලය අවුරුදු 1, මාස 0 සහ දින 1 කි. දෙවන සෙනරියන් ඩේට් ටයිම් හි දැන් 1/1/2002 වන අතර දරුවා 1/1/2001 දින උපත ලබයි. මෙම අවස්ථාවේ දී ප්රති result ලය වසර 1, මාස 0 සහ දින 0 ක් වනු ඇත. එය සිදුවන්නේ ඔබ අධික නොවන වසරක කාල පරාසය එකතු කරන බැවිනි. DateTime.MinValue අධික වසරක් නම්, ප්‍රති results ල පළමු වසරට වසර 1 ක් සහ අවුරුදු 0 මාස 11 සහ දින 30 ක් වනු ඇත. (එය ඔබේ කේතයෙන් උත්සාහ කරන්න).
අතනාසියෝස් කතරස්

1
ඉහළට! මම බොහෝ දුරට සමාන විසඳුමක් ඉදිරිපත් කළෙමි (මම + වෙනුවට DateTime.MinValue.AddTicks (span.Ticks) භාවිතා කළෙමි, නමුත් ප්‍රති result ලය සමාන වන අතර ඔබේ අක්ෂර කිහිපයක් අඩු කේතයක් ඇත).
මාකෝටෝසන්

4
ඔබ හරි, එය එසේ නොවේ. නමුත් එය එසේ නම් එහි ප්‍රති .ලය වනු ඇත. එය වැදගත් වන්නේ ඇයි? එය එසේ නොවේ. කෙසේ වෙතත් පිම්ම හෝ නැතත් මෙය ක්‍රියාත්මක නොවන උදාහරණ ඇත. මට පෙන්වීමට අවශ්‍ය වූයේ එයයි. DIFF නිවැරදි ය. ස්පාන් පිම්ම වසර ගණන් ගනී. නමුත් මූලික දිනයකට එකතු කිරීම එසේ නොවේ. කේතයේ ඇති උදාහරණ උත්සාහ කරන්න, එවිට මම නිවැරදි බව ඔබට පෙනෙනු ඇත.
අතනාසියෝස් කතරස්

29

යමෙකුගේ උපන්දිනය අනුව යමෙකුගේ වයස ගණනය කිරීම සඳහා මම SQL සේවාදායක පරිශීලක අර්ථ දැක්වීමේ කාර්යයක් නිර්මාණය කර ඇත්තෙමි. විමසුමක කොටසක් ලෙස ඔබට එය අවශ්‍ය වූ විට මෙය ප්‍රයෝජනවත් වේ:

using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
    [SqlFunction(DataAccess = DataAccessKind.Read)]
    public static SqlInt32 CalculateAge(string strBirthDate)
    {
        DateTime dtBirthDate = new DateTime();
        dtBirthDate = Convert.ToDateTime(strBirthDate);
        DateTime dtToday = DateTime.Now;

        // get the difference in years
        int years = dtToday.Year - dtBirthDate.Year;

        // subtract another year if we're before the
        // birth day in the current year
        if (dtToday.Month < dtBirthDate.Month || (dtToday.Month == dtBirthDate.Month && dtToday.Day < dtBirthDate.Day))
            years=years-1;

        int intCustomerAge = years;
        return intCustomerAge;
    }
};

28

මෙන්න තවත් පිළිතුරක්:

public static int AgeInYears(DateTime birthday, DateTime today)
{
    return ((today.Year - birthday.Year) * 372 + (today.Month - birthday.Month) * 31 + (today.Day - birthday.Day)) / 372;
}

මෙය පුළුල් ලෙස ඒකක පරීක්ෂාවට ලක් කර ඇත. එය ටිකක් "මැජික්" ලෙස පෙනේ. අංක 372 යනු සෑම මසකම දින 31 ක් තිබේ නම් වසරක් තුළ ගතවන දින ගණනයි.

එය ක්‍රියාත්මක වන්නේ ඇයිද යන්න පැහැදිලි කිරීම ( මෙතැනින් ඔසවා ):

අපි සකස් කරමු Yn = DateTime.Now.Year, Yb = birthday.Year, Mn = DateTime.Now.Month, Mb = birthday.Month, Dn = DateTime.Now.Day, Db = birthday.Day

age = Yn - Yb + (31*(Mn - Mb) + (Dn - Db)) / 372

අපට අවශ්‍ය වන්නේ එක්කෝ Yn-Ybදිනය දැනටමත් ළඟා වී ඇත්නම් හෝ නොමැති නම් බව අපි දනිමු Yn-Yb-1.

අ) Mn<Mbඅපට තිබේ නම්-341 <= 31*(Mn-Mb) <= -31 and -30 <= Dn-Db <= 30

-371 <= 31*(Mn - Mb) + (Dn - Db) <= -1

පූර්ණ සංඛ්‍යා බෙදීම සමඟ

(31*(Mn - Mb) + (Dn - Db)) / 372 = -1

ආ) නම් Mn=Mbහා Dn<Db, අපි31*(Mn - Mb) = 0 and -30 <= Dn-Db <= -1

පූර්ණ සංඛ්‍යා බෙදීම සමඟ, නැවතත්

(31*(Mn - Mb) + (Dn - Db)) / 372 = -1

ඇ) එසේ නම් Mn>Mb, අපට තිබේ31 <= 31*(Mn-Mb) <= 341 and -30 <= Dn-Db <= 30

1 <= 31*(Mn - Mb) + (Dn - Db) <= 371

පූර්ණ සංඛ්‍යා බෙදීම සමඟ

(31*(Mn - Mb) + (Dn - Db)) / 372 = 0

ඈ) නම් Mn=Mbහා Dn>Db, අපි 31*(Mn - Mb) = 0 and 1 <= Dn-Db <= 30

පූර්ණ සංඛ්‍යා බෙදීම සමඟ, නැවතත්

(31*(Mn - Mb) + (Dn - Db)) / 372 = 0

ඉ) නම් Mn=Mbහා Dn=Db, අපි31*(Mn - Mb) + Dn-Db = 0

ඒ නිසා (31*(Mn - Mb) + (Dn - Db)) / 372 = 0


3
මම මෙම දීර් and හා කරදරකාරී සාකච්ඡාවට පැටලී සිටියෙමි, ඔබේ විසඳුම ඇත්තෙන්ම හොඳ හා කුඩා ප්‍රවේශයකි. එය සරලව තබා ගැනීම ගැන ස්තූතියි
nabuchodonossor

25

මම මේ සඳහා යම් කාලයක් ගත කර ඇති අතර, කෙනෙකුගේ වයස අවුරුදු, මාස සහ දින ගණනය කිරීම සඳහා මෙය ඉදිරිපත් කර ඇත්තෙමි. මම පෙබරවාරි 29 වන දින ගැටළුවට සහ වසර ගණනාවකට එරෙහිව අත්හදා බැලුවෙමි.

public void LoopAge(DateTime myDOB, DateTime FutureDate)
{
    int years = 0;
    int months = 0;
    int days = 0;

    DateTime tmpMyDOB = new DateTime(myDOB.Year, myDOB.Month, 1);

    DateTime tmpFutureDate = new DateTime(FutureDate.Year, FutureDate.Month, 1);

    while (tmpMyDOB.AddYears(years).AddMonths(months) < tmpFutureDate)
    {
        months++;

        if (months > 12)
        {
            years++;
            months = months - 12;
        }
    }

    if (FutureDate.Day >= myDOB.Day)
    {
        days = days + FutureDate.Day - myDOB.Day;
    }
    else
    {
        months--;

        if (months < 0)
        {
            years--;
            months = months + 12;
        }

        days +=
            DateTime.DaysInMonth(
                FutureDate.AddMonths(-1).Year, FutureDate.AddMonths(-1).Month
            ) + FutureDate.Day - myDOB.Day;

    }

    //add an extra day if the dob is a leap day
    if (DateTime.IsLeapYear(myDOB.Year) && myDOB.Month == 2 && myDOB.Day == 29)
    {
        //but only if the future date is less than 1st March
        if (FutureDate >= new DateTime(FutureDate.Year, 3, 1))
            days++;
    }

}

21

අවුරුදු 1 ට වඩා අඩු පුද්ගලයින් ගැන අප සලකා බැලිය යුතුද? චීන සංස්කෘතිය ලෙස, අපි කුඩා ළදරුවන්ගේ වයස මාස 2 ක් හෝ සති 4 ක් ලෙස විස්තර කරමු.

පහත දැක්වෙන්නේ මගේ ක්‍රියාවට නැංවීමයි, එය මා සිතූ තරම් සරල නැත, විශේෂයෙන් 2/28 වැනි දිනය සමඟ කටයුතු කිරීම.

public static string HowOld(DateTime birthday, DateTime now)
{
    if (now < birthday)
        throw new ArgumentOutOfRangeException("birthday must be less than now.");

    TimeSpan diff = now - birthday;
    int diffDays = (int)diff.TotalDays;

    if (diffDays > 7)//year, month and week
    {
        int age = now.Year - birthday.Year;

        if (birthday > now.AddYears(-age))
            age--;

        if (age > 0)
        {
            return age + (age > 1 ? " years" : " year");
        }
        else
        {// month and week
            DateTime d = birthday;
            int diffMonth = 1;

            while (d.AddMonths(diffMonth) <= now)
            {
                diffMonth++;
            }

            age = diffMonth-1;

            if (age == 1 && d.Day > now.Day)
                age--;

            if (age > 0)
            {
                return age + (age > 1 ? " months" : " month");
            }
            else
            {
                age = diffDays / 7;
                return age + (age > 1 ? " weeks" : " week");
            }
        }
    }
    else if (diffDays > 0)
    {
        int age = diffDays;
        return age + (age > 1 ? " days" : " day");
    }
    else
    {
        int age = diffDays;
        return "just born";
    }
}

මෙම ක්‍රියාත්මක කිරීම පරීක්ෂණ අවස්ථා වලට වඩා පහතින් සමත් වී ඇත.

[TestMethod]
public void TestAge()
{
    string age = HowOld(new DateTime(2011, 1, 1), new DateTime(2012, 11, 30));
    Assert.AreEqual("1 year", age);

    age = HowOld(new DateTime(2011, 11, 30), new DateTime(2012, 11, 30));
    Assert.AreEqual("1 year", age);

    age = HowOld(new DateTime(2001, 1, 1), new DateTime(2012, 11, 30));
    Assert.AreEqual("11 years", age);

    age = HowOld(new DateTime(2012, 1, 1), new DateTime(2012, 11, 30));
    Assert.AreEqual("10 months", age);

    age = HowOld(new DateTime(2011, 12, 1), new DateTime(2012, 11, 30));
    Assert.AreEqual("11 months", age);

    age = HowOld(new DateTime(2012, 10, 1), new DateTime(2012, 11, 30));
    Assert.AreEqual("1 month", age);

    age = HowOld(new DateTime(2008, 2, 28), new DateTime(2009, 2, 28));
    Assert.AreEqual("1 year", age);

    age = HowOld(new DateTime(2008, 3, 28), new DateTime(2009, 2, 28));
    Assert.AreEqual("11 months", age);

    age = HowOld(new DateTime(2008, 3, 28), new DateTime(2009, 3, 28));
    Assert.AreEqual("1 year", age);

    age = HowOld(new DateTime(2009, 1, 28), new DateTime(2009, 2, 28));
    Assert.AreEqual("1 month", age);

    age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 3, 1));
    Assert.AreEqual("1 month", age);

    // NOTE.
    // new DateTime(2008, 1, 31).AddMonths(1) == new DateTime(2009, 2, 28);
    // new DateTime(2008, 1, 28).AddMonths(1) == new DateTime(2009, 2, 28);
    age = HowOld(new DateTime(2009, 1, 31), new DateTime(2009, 2, 28));
    Assert.AreEqual("4 weeks", age);

    age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 2, 28));
    Assert.AreEqual("3 weeks", age);

    age = HowOld(new DateTime(2009, 2, 1), new DateTime(2009, 3, 1));
    Assert.AreEqual("1 month", age);

    age = HowOld(new DateTime(2012, 11, 5), new DateTime(2012, 11, 30));
    Assert.AreEqual("3 weeks", age);

    age = HowOld(new DateTime(2012, 11, 1), new DateTime(2012, 11, 30));
    Assert.AreEqual("4 weeks", age);

    age = HowOld(new DateTime(2012, 11, 20), new DateTime(2012, 11, 30));
    Assert.AreEqual("1 week", age);

    age = HowOld(new DateTime(2012, 11, 25), new DateTime(2012, 11, 30));
    Assert.AreEqual("5 days", age);

    age = HowOld(new DateTime(2012, 11, 29), new DateTime(2012, 11, 30));
    Assert.AreEqual("1 day", age);

    age = HowOld(new DateTime(2012, 11, 30), new DateTime(2012, 11, 30));
    Assert.AreEqual("just born", age);

    age = HowOld(new DateTime(2000, 2, 29), new DateTime(2009, 2, 28));
    Assert.AreEqual("8 years", age);

    age = HowOld(new DateTime(2000, 2, 29), new DateTime(2009, 3, 1));
    Assert.AreEqual("9 years", age);

    Exception e = null;

    try
    {
        age = HowOld(new DateTime(2012, 12, 1), new DateTime(2012, 11, 30));
    }
    catch (ArgumentOutOfRangeException ex)
    {
        e = ex;
    }

    Assert.IsTrue(e != null);
}

එය ප්‍රයෝජනවත් යැයි සිතමි.


20

එය සරළව තබා ගැනීම (හා සමහර විට මෝඩ :)).

DateTime birth = new DateTime(1975, 09, 27, 01, 00, 00, 00);
TimeSpan ts = DateTime.Now - birth;
Console.WriteLine("You are approximately " + ts.TotalSeconds.ToString() + " seconds old.");

ටයිම්ස්පන් මගේ පළමු තේරීම විය, නමුත් එය ටෝල්ටයියර්ස් දේපලක් ලබා නොදෙන බව සොයා ගත්තේය. ඔබට උත්සාහ කළ හැකිය (ts.TotalDays / 365) - නමුත් එය අධික වසර ගණනක් සඳහා ගණන් නොගනී.
Lazlow

19

මම මෙතෙක් සොයාගෙන ඇති සරලම ක්‍රමය මෙයයි. එය එක්සත් ජනපද සහ බටහිර යුරෝපා ප්‍රදේශ සඳහා නිවැරදිව ක්‍රියා කරයි. වෙනත් ප්‍රදේශ සමඟ, විශේෂයෙන් චීනය වැනි ස්ථාන සමඟ කතා කළ නොහැක. අමතර සංසන්දනයන් 4 ක්, බොහෝ විට, වයස ගණනය කිරීමෙන් පසුව.

public int AgeInYears(DateTime birthDate, DateTime referenceDate)
{
  Debug.Assert(referenceDate >= birthDate, 
               "birth date must be on or prior to the reference date");

  DateTime birth = birthDate.Date;
  DateTime reference = referenceDate.Date;
  int years = (reference.Year - birth.Year);

  //
  // an offset of -1 is applied if the birth date has 
  // not yet occurred in the current year.
  //
  if (reference.Month > birth.Month);
  else if (reference.Month < birth.Month) 
    --years;
  else // in birth month
  {
    if (reference.Day < birth.Day)
      --years;
  }

  return years ;
}

මම මේ සඳහා පිළිතුරු සොයා බලමින් සිටි අතර අධික දින උපත් වල නියාමන / නෛතික ඇඟවුම් පිළිබඳව කිසිවෙකු සඳහන් කර නොමැති බව මම දුටුවෙමි. නිදසුනක් වශයෙන්, විකිපීඩියාවකට අනුව , ඔබ පෙබරවාරි 29 වන දින විවිධ අධිකරණ බල ප්‍රදේශවල උපත ලැබුවහොත්, ඔබ අධික නොවන අවුරුදු උපන් දිනය වෙනස් වේ:

  • එක්සත් රාජධානියේ සහ හොංකොං හි: එය වසරේ සාමාන්‍ය දිනයයි, එබැවින් ඊළඟ දිනය මාර්තු 1 ඔබේ උපන් දිනයයි.
  • නවසීලන්තයේ: එය පෙර දින, රියදුරු බලපත්‍ර ලබා ගැනීම සඳහා පෙබරවාරි 28 වන දින සහ වෙනත් අරමුණු සඳහා මාර්තු 1 වනදා ය.
  • තායිවානය: එය පෙබරවාරි 28 යි.

මට කිව හැකි තරම් ආසන්න වශයෙන්, එක්සත් ජනපදයේ, ප්‍ර utes ප්ති මේ සම්බන්ධයෙන් නිහ are ව සිටින අතර, එය පොදු නීතියට සහ විවිධ නියාමන ආයතන ඔවුන්ගේ රෙගුලාසි වල දේවල් නිර්වචනය කරන්නේ කෙසේද යන්න දක්වා ඇත.

ඒ සඳහා වැඩිදියුණු කිරීමක්:

public enum LeapDayRule
{
  OrdinalDay     = 1 ,
  LastDayOfMonth = 2 ,
}

static int ComputeAgeInYears(DateTime birth, DateTime reference, LeapYearBirthdayRule ruleInEffect)
{
  bool isLeapYearBirthday = CultureInfo.CurrentCulture.Calendar.IsLeapDay(birth.Year, birth.Month, birth.Day);
  DateTime cutoff;

  if (isLeapYearBirthday && !DateTime.IsLeapYear(reference.Year))
  {
    switch (ruleInEffect)
    {
      case LeapDayRule.OrdinalDay:
        cutoff = new DateTime(reference.Year, 1, 1)
                             .AddDays(birth.DayOfYear - 1);
        break;

      case LeapDayRule.LastDayOfMonth:
        cutoff = new DateTime(reference.Year, birth.Month, 1)
                             .AddMonths(1)
                             .AddDays(-1);
        break;

      default:
        throw new InvalidOperationException();
    }
  }
  else
  {
    cutoff = new DateTime(reference.Year, birth.Month, birth.Day);
  }

  int age = (reference.Year - birth.Year) + (reference >= cutoff ? 0 : -1);
  return age < 0 ? 0 : age;
}

මෙම කේතය උපකල්පනය කරන බව සැලකිල්ලට ගත යුතුය:

  • බටහිර (යුරෝපීය) වයස ගණනය කිරීම, සහ
  • ග්‍රෙගෝරියන් දින දර්ශනය වැනි දින දර්ශනයක්, මාසයක් අවසානයේ එක් පිම්ම දිනයක් ඇතුළත් කරයි.

19
TimeSpan diff = DateTime.Now - birthdayDateTime;
string age = String.Format("{0:%y} years, {0:%M} months, {0:%d}, days old", diff);

එය ඔබ වෙත නැවත පැමිණීමට ඔබ කැමති දැයි මට විශ්වාස නැත, එබැවින් මම කියවිය හැකි නූලක් සෑදුවෙමි.


18

මෙය answer ජු පිළිතුරක් නොව, අර්ධ විද්‍යාත්මක දෘෂ්ටි කෝණයකින් ඇති ගැටලුව පිළිබඳ දාර්ශනික තර්කනයකි.

ප්රශ්නය වයස මැනීම සඳහා ඒකකය හෝ සංස්කෘතිය නිශ්චිතව දක්වා නැති බව මම තර්ක කරමි, බොහෝ පිළිතුරු පූර්ණ සංඛ්‍යා වාර්ෂික නිරූපණයක් උපකල්පනය කරයි. කාලය සඳහා වන SI- ඒකකය නම් second, නිවැරදි ජනක පිළිතුර විය යුතුය (ඇත්ත වශයෙන්ම සාමාන්‍යකරණය යැයි උපකල්පනය DateTimeකර සාපේක්ෂතාවාදී බලපෑම් කෙරෙහි කිසිදු තැකීමක් නොකරයි):

var lifeInSeconds = (DateTime.Now.Ticks - then.Ticks)/TickFactor;

අවුරුදු ගණන ගණනය කිරීමේ කිතුනු ක්‍රමයට:

var then = ... // Then, in this case the birthday
var now = DateTime.UtcNow;
int age = now.Year - then.Year;
if (now.AddYears(-age) < then) age--;

බොහෝ විට දිවා ගණන් භාගය ලෙස හඳුන්වන දෙයක් ගණනය කිරීමේදී මූල්‍යමය වශයෙන් සමාන ගැටළුවක් ඇති අතර එය දළ වශයෙන් යම් කාල පරිච්ඡේදයක් සඳහා වසර ගණනකි. වයස් ප්‍රශ්නය ඇත්ත වශයෙන්ම කාලය මැනීමේ ගැටලුවකි.

තථ්‍ය / තථ්‍ය (සෑම දිනකම "නිවැරදිව" ගණන් කිරීම) සඳහා උදාහරණය:

DateTime start, end = .... // Whatever, assume start is before end

double startYearContribution = 1 - (double) start.DayOfYear / (double) (DateTime.IsLeapYear(start.Year) ? 366 : 365);
double endYearContribution = (double)end.DayOfYear / (double)(DateTime.IsLeapYear(end.Year) ? 366 : 365);
double middleContribution = (double) (end.Year - start.Year - 1);

double DCF = startYearContribution + endYearContribution + middleContribution;

සාමාන්‍යයෙන් කාලය මැනීම සඳහා තවත් පොදු ක්‍රමයක් වන්නේ “අනුක්‍රමිකකරණය” කිරීමයි (මෙම දිනය සම්මුතිය නම් කළ මචන් බැරෑරුම් ලෙස ට්‍රිපින් විය යුතුය):

DateTime start, end = .... // Whatever, assume start is before end
int days = (end - start).Days;

මේ දක්වා කෙනෙකුගේ ජීවිත කාලය තුළ පෘථිවිය වටා ඇති සූර්ය චක්‍ර දළ වශයෙන් ආසන්න වශයෙන් ගණනය කිරීමට වඩා තත්පරයකින් සාපේක්‍ෂ යුගයකට පෙර අපට කොපමණ කාලයක් යා යුතුද යන්න මට සිතේ. :) හෝ වෙනත් වචන වලින් කිවහොත්, යම් කාල පරිච්ඡේදයක් සඳහා ස්ථානයක් ලබා දිය යුතු විට හෝ චලිතය වලංගු වීමට නිරූපණය කරන ශ්‍රිතයක් :)


TickFactor යනු කුමක්ද?
අනර්

තත්පරයට කිනිතුල්ලන් තත්පරයට කිනිතුල්ලන් සාමාන්‍යකරණය කිරීමට භාවිතා කරයි.
flindeberg

17

මෙන්න විසඳුමක්.

DateTime dateOfBirth = new DateTime(2000, 4, 18);
DateTime currentDate = DateTime.Now;

int ageInYears = 0;
int ageInMonths = 0;
int ageInDays = 0;

ageInDays = currentDate.Day - dateOfBirth.Day;
ageInMonths = currentDate.Month - dateOfBirth.Month;
ageInYears = currentDate.Year - dateOfBirth.Year;

if (ageInDays < 0)
{
    ageInDays += DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
    ageInMonths = ageInMonths--;

    if (ageInMonths < 0)
    {
        ageInMonths += 12;
        ageInYears--;
    }
}

if (ageInMonths < 0)
{
    ageInMonths += 12;
    ageInYears--;
}

Console.WriteLine("{0}, {1}, {2}", ageInYears, ageInMonths, ageInDays);

නූල් කොන්ක්‍රීට් සමඟ මෙය කළ හැකි ය: 47 අවුරුදු 11 මෝ 7 දින
ජොෂ්යෙට්ස් 195

16

පෙබරවාරි 28 වන දිනට සාපේක්ෂව පෙබරවාරි 29 වන දින උපන් දිනය නිරාකරණය කර ගත හැකි වඩාත් නිවැරදි පිළිතුර මෙයයි.

public int GetAge(DateTime birthDate)
{
    int age = DateTime.Now.Year - birthDate.Year;

    if (birthDate.DayOfYear > DateTime.Now.DayOfYear)
        age--;

    return age;
}




එය අදයි! (ඊළඟ එක දැන් සිට අවුරුදු හතරකි.)
පීටර් මෝර්ටෙන්සන්

15

වයස ගණනය කිරීම සඳහා මට අභිමතකරණය කළ ක්‍රමවේදයක් ඇත, තවද එය උදව් වන විට ප්‍රසාද වලංගු කිරීමේ පණිවිඩයක්:

public void GetAge(DateTime dob, DateTime now, out int years, out int months, out int days)
{
    years = 0;
    months = 0;
    days = 0;

    DateTime tmpdob = new DateTime(dob.Year, dob.Month, 1);
    DateTime tmpnow = new DateTime(now.Year, now.Month, 1);

    while (tmpdob.AddYears(years).AddMonths(months) < tmpnow)
    {
        months++;
        if (months > 12)
        {
            years++;
            months = months - 12;
        }
    }

    if (now.Day >= dob.Day)
        days = days + now.Day - dob.Day;
    else
    {
        months--;
        if (months < 0)
        {
            years--;
            months = months + 12;
        }
        days += DateTime.DaysInMonth(now.AddMonths(-1).Year, now.AddMonths(-1).Month) + now.Day - dob.Day;
    }

    if (DateTime.IsLeapYear(dob.Year) && dob.Month == 2 && dob.Day == 29 && now >= new DateTime(now.Year, 3, 1))
        days++;

}   

private string ValidateDate(DateTime dob) //This method will validate the date
{
    int Years = 0; int Months = 0; int Days = 0;

    GetAge(dob, DateTime.Now, out Years, out Months, out Days);

    if (Years < 18)
        message =  Years + " is too young. Please try again on your 18th birthday.";
    else if (Years >= 65)
        message = Years + " is too old. Date of Birth must not be 65 or older.";
    else
        return null; //Denotes validation passed
}

ක්‍රමය මෙතැනින් අමතා දිවා කාලයේ අගය ලබා දෙන්න (සේවාදායකය ඇමරිකා එක්සත් ජනපදයට සකසා ඇත්නම් MM / dd / yyyy). මෙය පෙන්වීමට පණිවිඩ පෙට්ටියක් හෝ ඕනෑම බහාලුමක් සමඟ ප්‍රතිස්ථාපනය කරන්න:

DateTime dob = DateTime.Parse("03/10/1982");  

string message = ValidateDate(dob);

lbldatemessage.Visible = !StringIsNullOrWhitespace(message);
lbldatemessage.Text = message ?? ""; //Ternary if message is null then default to empty string

ඔබට කැමති ආකාරයකින් පණිවිඩය සංයුති කළ හැකි බව මතක තබා ගන්න.


14

මෙම විසඳුම ගැන කුමක් කිව හැකිද?

static string CalcAge(DateTime birthDay)
{
    DateTime currentDate = DateTime.Now;         
    int approximateAge = currentDate.Year - birthDay.Year;
    int daysToNextBirthDay = (birthDay.Month * 30 + birthDay.Day) - 
        (currentDate.Month * 30 + currentDate.Day) ;

    if (approximateAge == 0 || approximateAge == 1)
    {                
        int month =  Math.Abs(daysToNextBirthDay / 30);
        int days = Math.Abs(daysToNextBirthDay % 30);

        if (month == 0)
            return "Your age is: " + daysToNextBirthDay + " days";

        return "Your age is: " + month + " months and " + days + " days"; ;
    }

    if (daysToNextBirthDay > 0)
        return "Your age is: " + --approximateAge + " Years";

    return "Your age is: " + approximateAge + " Years"; ;
}

12
private int GetAge(int _year, int _month, int _day
{
    DateTime yourBirthDate= new DateTime(_year, _month, _day);

    DateTime todaysDateTime = DateTime.Today;
    int noOfYears = todaysDateTime.Year - yourBirthDate.Year;

    if (DateTime.Now.Month < yourBirthDate.Month ||
        (DateTime.Now.Month == yourBirthDate.Month && DateTime.Now.Day < yourBirthDate.Day))
    {
        noOfYears--;
    }

    return  noOfYears;
}

10

පහත දැක්වෙන ප්‍රවේශය ( .NET පන්තියේ දිනය ඩිෆ් සඳහා කාල පරිච්ඡේද පුස්තකාලයෙන් උපුටා ගැනීම ) සංස්කෘතික තොරතුරු වල දින දර්ශනය සලකා බලයි:

// ----------------------------------------------------------------------
private static int YearDiff( DateTime date1, DateTime date2 )
{
  return YearDiff( date1, date2, DateTimeFormatInfo.CurrentInfo.Calendar );
} // YearDiff

// ----------------------------------------------------------------------
private static int YearDiff( DateTime date1, DateTime date2, Calendar calendar )
{
  if ( date1.Equals( date2 ) )
  {
    return 0;
  }

  int year1 = calendar.GetYear( date1 );
  int month1 = calendar.GetMonth( date1 );
  int year2 = calendar.GetYear( date2 );
  int month2 = calendar.GetMonth( date2 );

  // find the the day to compare
  int compareDay = date2.Day;
  int compareDaysPerMonth = calendar.GetDaysInMonth( year1, month1 );
  if ( compareDay > compareDaysPerMonth )
  {
    compareDay = compareDaysPerMonth;
  }

  // build the compare date
  DateTime compareDate = new DateTime( year1, month2, compareDay,
    date2.Hour, date2.Minute, date2.Second, date2.Millisecond );
  if ( date2 > date1 )
  {
    if ( compareDate < date1 )
    {
      compareDate = compareDate.AddYears( 1 );
    }
  }
  else
  {
    if ( compareDate > date1 )
    {
      compareDate = compareDate.AddYears( -1 );
    }
  }
  return year2 - calendar.GetYear( compareDate );
} // YearDiff

භාවිතය:

// ----------------------------------------------------------------------
public void CalculateAgeSamples()
{
  PrintAge( new DateTime( 2000, 02, 29 ), new DateTime( 2009, 02, 28 ) );
  // > Birthdate=29.02.2000, Age at 28.02.2009 is 8 years
  PrintAge( new DateTime( 2000, 02, 29 ), new DateTime( 2012, 02, 28 ) );
  // > Birthdate=29.02.2000, Age at 28.02.2012 is 11 years
} // CalculateAgeSamples

// ----------------------------------------------------------------------
public void PrintAge( DateTime birthDate, DateTime moment )
{
  Console.WriteLine( "Birthdate={0:d}, Age at {1:d} is {2} years", birthDate, moment, YearDiff( birthDate, moment ) );
} // PrintAge

10

මෙම සම්භාව්‍ය ප්‍රශ්නය නෝඩා ටයිම් විසඳුමකට සුදුසු ය .

static int GetAge(LocalDate dateOfBirth)
{
    Instant now = SystemClock.Instance.Now;

    // The target time zone is important.
    // It should align with the *current physical location* of the person
    // you are talking about.  When the whereabouts of that person are unknown,
    // then you use the time zone of the person who is *asking* for the age.
    // The time zone of birth is irrelevant!

    DateTimeZone zone = DateTimeZoneProviders.Tzdb["America/New_York"];

    LocalDate today = now.InZone(zone).Date;

    Period period = Period.Between(dateOfBirth, today, PeriodUnits.Years);

    return (int) period.Years;
}

භාවිතය:

LocalDate dateOfBirth = new LocalDate(1976, 8, 27);
int age = GetAge(dateOfBirth);

පහත සඳහන් වැඩිදියුණු කිරීම් ගැන ද ඔබ උනන්දු විය හැකිය:

  • IClockභාවිතා කිරීම වෙනුවට ඔරලෝසුව තුළ ගමන් SystemClock.Instanceකිරීම පරීක්ෂණ හැකියාව වැඩි දියුණු කරයි.

  • ඉලක්කගත කාල කලාපය වෙනස් වීමට ඉඩ ඇත, එබැවින් ඔබට DateTimeZoneපරාමිතියක් ද අවශ්‍ය වේ.

මෙම මාතෘකාව පිළිබඳ මගේ බ්ලොග් සටහන ද බලන්න: උපන් දින හැසිරවීම සහ වෙනත් සංවත්සර


ඔබ නෝඩා ටයිම් සමඟ අනුබද්ධද?
සිමානෝ

මම ඒ සඳහා දායක මුදල් ලබා දී ඇත, නමුත් එය මූලික වශයෙන් ජෝන් ස්කීට්ගේ ය.
මැට් ජොන්සන්-පින්ට්

9

පුද්ගලයන්ගේ වයස නිවැරදිව ගණනය කිරීම සඳහා මම ScArcher2 හි විසඳුම භාවිතා කළ නමුත් එය තවදුරටත් ගෙන ගොස් ඔවුන්ගේ මාස සහ දින ගණනය කිරීම අවශ්‍ය විය.

    public static Dictionary<string,int> CurrentAgeInYearsMonthsDays(DateTime? ndtBirthDate, DateTime? ndtReferralDate)
    {
        //----------------------------------------------------------------------
        // Can't determine age if we don't have a dates.
        //----------------------------------------------------------------------
        if (ndtBirthDate == null) return null;
        if (ndtReferralDate == null) return null;

        DateTime dtBirthDate = Convert.ToDateTime(ndtBirthDate);
        DateTime dtReferralDate = Convert.ToDateTime(ndtReferralDate);

        //----------------------------------------------------------------------
        // Create our Variables
        //----------------------------------------------------------------------
        Dictionary<string, int> dYMD = new Dictionary<string,int>();
        int iNowDate, iBirthDate, iYears, iMonths, iDays;
        string sDif = "";

        //----------------------------------------------------------------------
        // Store off current date/time and DOB into local variables
        //---------------------------------------------------------------------- 
        iNowDate = int.Parse(dtReferralDate.ToString("yyyyMMdd"));
        iBirthDate = int.Parse(dtBirthDate.ToString("yyyyMMdd"));

        //----------------------------------------------------------------------
        // Calculate Years
        //----------------------------------------------------------------------
        sDif = (iNowDate - iBirthDate).ToString();
        iYears = int.Parse(sDif.Substring(0, sDif.Length - 4));

        //----------------------------------------------------------------------
        // Store Years in Return Value
        //----------------------------------------------------------------------
        dYMD.Add("Years", iYears);

        //----------------------------------------------------------------------
        // Calculate Months
        //----------------------------------------------------------------------
        if (dtBirthDate.Month > dtReferralDate.Month)
            iMonths = 12 - dtBirthDate.Month + dtReferralDate.Month - 1;
        else
            iMonths = dtBirthDate.Month - dtReferralDate.Month;

        //----------------------------------------------------------------------
        // Store Months in Return Value
        //----------------------------------------------------------------------
        dYMD.Add("Months", iMonths);

        //----------------------------------------------------------------------
        // Calculate Remaining Days
        //----------------------------------------------------------------------
        if (dtBirthDate.Day > dtReferralDate.Day)
            //Logic: Figure out the days in month previous to the current month, or the admitted month.
            //       Subtract the birthday from the total days which will give us how many days the person has lived since their birthdate day the previous month.
            //       then take the referral date and simply add the number of days the person has lived this month.

            //If referral date is january, we need to go back to the following year's December to get the days in that month.
            if (dtReferralDate.Month == 1)
                iDays = DateTime.DaysInMonth(dtReferralDate.Year - 1, 12) - dtBirthDate.Day + dtReferralDate.Day;       
            else
                iDays = DateTime.DaysInMonth(dtReferralDate.Year, dtReferralDate.Month - 1) - dtBirthDate.Day + dtReferralDate.Day;       
        else
            iDays = dtReferralDate.Day - dtBirthDate.Day;             

        //----------------------------------------------------------------------
        // Store Days in Return Value
        //----------------------------------------------------------------------
        dYMD.Add("Days", iDays);

        return dYMD;
}

9

SQL අනුවාදය:

declare @dd smalldatetime = '1980-04-01'
declare @age int = YEAR(GETDATE())-YEAR(@dd)
if (@dd> DATEADD(YYYY, -@age, GETDATE())) set @age = @age -1

print @age  

8

මාක් සොන්ගේ පිළිතුරට මම එක් කුඩා වෙනසක් කර ඇත : ප්‍රකාශනය මඳක් පහසුවෙන් විග්‍රහ කළ හැකි වන පරිදි මම තුන්වන පේළිය නැවත ලියා ඇත.

public int AgeInYears(DateTime bday)
{
    DateTime now = DateTime.Today;
    int age = now.Year - bday.Year;            
    if (bday.AddYears(age) > now) 
        age--;
    return age;
}

මම එය පැහැදිලි කිරීමක් සඳහා ශ්‍රිතයක් බවට පත් කර ඇත්තෙමි.

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.