DateTime
কোনও ব্যক্তির জন্মদিনের প্রতিনিধিত্ব করে দেওয়া , আমি কীভাবে কয়েক বছর ধরে তাদের বয়স গণনা করব?
DateTime
কোনও ব্যক্তির জন্মদিনের প্রতিনিধিত্ব করে দেওয়া , আমি কীভাবে কয়েক বছর ধরে তাদের বয়স গণনা করব?
উত্তর:
একটি সহজ এবং সহজ সমাধান।
// 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--;
তবে এটি ধরে নেয় আপনি বয়সের পশ্চিমা ধারণাটি খুঁজছেন এবং পূর্ব এশীয় গণনা ব্যবহার করছেন না ।
এটি করার একটি অদ্ভুত উপায়, তবে আপনি যদি তারিখটি ফর্ম্যাট করে yyyymmdd
এবং বর্তমান তারিখ থেকে জন্মের তারিখটি বিয়োগ করেন তবে আপনার বয়সটি শেষ 4 টি ছেড়ে দিন :)
আমি সি # জানি না, তবে আমি বিশ্বাস করি এটি কোনও ভাষায় কাজ করবে।
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;
}
20180101 - 20171231 = 8870
। শেষ 4 টি সংখ্যা ফেলে দিন এবং আপনার 0
বয়সের জন্য (অন্তর্ভুক্ত) । কীভাবে পেলেন 1
?
এখানে একটি পরীক্ষার স্নিপেট রয়েছে:
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;
}
এর সহজ উত্তরটি AddYears
নীচে দেখানো হিসাবে প্রয়োগ করা কারণ লিপ বছরের 29 শে ফেব্রুয়ারিতে বছর যুক্ত করার এবং সাধারণ বছরের 28 ফেব্রুয়ারির সঠিক ফলাফল অর্জনের একমাত্র দেশীয় পদ্ধতি।
কেউ কেউ মনে করেন যে ১ লা মার্চ লেপলিংয়ের জন্মদিন তবে তাও নয়। নেট বা কোনও সরকারী নিয়মই এটিকে সমর্থন করে না বা ফেব্রুয়ারিতে জন্ম নেওয়া কিছু লোকের কেন অন্য মাসে তাদের জন্মদিনের 75% হওয়া উচিত তা সাধারণ যুক্তি দিয়ে বোঝায় না।
আরও একটি বয়স পদ্ধতি নিজেকে এক্সটেনশন হিসাবে যুক্ত করার জন্য ধার দেয় DateTime
। এর মাধ্যমে আপনি সহজতম পদ্ধতিতে বয়স অর্জন করতে পারেন:
int 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
}
date.Age(other)
?
আমার পরামর্শ
int age = (int) ((DateTime.Now - bday).TotalDays/365.242199);
মনে হয় বছরটি সঠিক তারিখে পরিবর্তন হচ্ছে। (আমি 107 বছর বয়স পর্যন্ত পরীক্ষা করেছি)
days in a year = 365.242199
আর একটি ফাংশন, আমার দ্বারা নয় তবে ওয়েবে পাওয়া গেছে এবং এটি কিছুটা পরিমার্জন করেছে:
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;
}
আমার মনে আসা কেবল দুটি জিনিস: গ্রেগরিয়ান ক্যালেন্ডার ব্যবহার করে না এমন দেশগুলির লোকদের সম্পর্কে কী? ডেটটাইম.নাউ আমার মনে হয় সার্ভার-নির্দিষ্ট সংস্কৃতিতে রয়েছে। এশিয়ান ক্যালেন্ডার নিয়ে আসলে কাজ করার বিষয়ে আমার একেবারে শূন্য জ্ঞান আছে এবং ক্যালেন্ডারগুলির মধ্যে তারিখগুলি রূপান্তর করার কোনও সহজ উপায় আছে কিনা তা আমি জানি না তবে কেবলমাত্র যদি আপনি 4660 বছর ধরে এই চীনা ছেলেরা সম্পর্কে ভাবছেন :-)
2 সমাধানের প্রধান সমস্যাগুলি হ'ল:
1. সঠিক বয়স গণনা করুন - বছর, মাস, দিন ইত্যাদিতে
২. সাধারণভাবে বোধিত বয়স গণনা করুন - লোকেরা সাধারণত তাদের বয়স কতটা তা যত্নশীল করে না, চলতি বছরে তাদের জন্মদিন যখন হয় তখন কেবল যত্ন করে।
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 নোট:
আরও একটি নোট ... আমি এর জন্য 2 টি স্ট্যাটিক ওভারলোডেড পদ্ধতি তৈরি করব, সর্বজনীন ব্যবহারের জন্য একটি, ব্যবহার-বন্ধুত্বের জন্য দ্বিতীয়:
public static int GetAge(DateTime bithDay, DateTime today)
{
//chosen solution method body
}
public static int GetAge(DateTime birthDay)
{
return GetAge(birthDay, DateTime.Now);
}
এখানে একটি ওয়ান-লাইনার:
int age = new DateTime(DateTime.Now.Subtract(birthday).Ticks).Year-1;
এটি আমাদের এখানে ব্যবহার করা সংস্করণ। এটি কাজ করে, এবং এটি মোটামুটি সহজ। এটি জেফের মত একই ধারণা তবে আমি মনে করি এটি কিছুটা স্পষ্ট কারণ এটি একটি বিয়োগের জন্য যুক্তিটিকে আলাদা করে, তাই এটি বুঝতে একটু সহজ।
public static int GetAge(this DateTime dateOfBirth, DateTime dateAsAt)
{
return dateAsAt.Year - dateOfBirth.Year - (dateOfBirth.DayOfYear < dateAsAt.DayOfYear ? 0 : 1);
}
আপনি যদি এই ধরণের বিষয়টিকে অস্পষ্ট বলে মনে করেন তবে এটি আরও পরিষ্কার করার জন্য আপনি টার্নারি অপারেটরটি প্রসারিত করতে পারেন।
স্পষ্টতই এটি একটি এক্সটেনশন পদ্ধতি হিসাবে সম্পন্ন হয়েছে DateTime
তবে আপনি স্পষ্টতই কোডটির একটি লাইন ধরে ফেলতে পারেন যা কাজ করে এবং যে কোনও জায়গায় রেখে দেয়। এখানে আমাদের কাছে এক্সটেনশন পদ্ধতির আরও একটি ওভারলোড রয়েছে যা DateTime.Now
কেবল সম্পূর্ণতার জন্য।
লিপ বছরগুলি এবং সমস্ত কিছুর কারণে আমি সবচেয়ে ভাল উপায়টি জানি is
DateTime birthDate = new DateTime(2000,3,1);
int age = (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.25D);
আমি এটি ব্যবহার:
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;
}
}
এটি এই প্রশ্নের "আরও বিশদ" দেয়। আপনি এটি যা খুঁজছেন হতে পারে
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);
কারও জন্মতারিখ গণনা করে আমি কোনও এসকিউএল সার্ভার ব্যবহারকারী সংজ্ঞায়িত ফাংশন তৈরি করেছি। এটি যখন আপনার কোনও প্রশ্নের অংশ হিসাবে প্রয়োজন তখন এটি দরকারী:
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;
}
};
এখানে আরও একটি উত্তর এখানে রয়েছে:
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
d) যদি
Mn=Mb
এবংDn>Db
, আমাদের31*(Mn - Mb) = 0 and 1 <= Dn-Db <= 3
0 আছেআবার পূর্ণসংখ্যা বিভাগের সাথে
(31*(Mn - Mb) + (Dn - Db)) / 372 = 0
e) যদি
Mn=Mb
এবংDn=Db
, আমাদের আছে31*(Mn - Mb) + Dn-Db = 0
এবং সেইজন্য
(31*(Mn - Mb) + (Dn - Db)) / 372 = 0
আমি এটিতে কাজ করার জন্য কিছু সময় ব্যয় করেছি এবং বছর, মাস এবং দিনগুলিতে কারও বয়স গণনা করার জন্য এটি নিয়ে এসেছি। আমি 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++;
}
}
আমাদের কি 1 বছরের চেয়ে কম বয়সীদের বিবেচনা করা দরকার? চাইনিজ সংস্কৃতি হিসাবে, আমরা ছোট বাচ্চাদের বয়স 2 মাস বা 4 সপ্তাহ হিসাবে বর্ণনা করি।
নীচে আমার বাস্তবায়ন হল, আমি যা কল্পনা করেছি তার মতো সহজ নয়, বিশেষত ২/২৮ তারিখের সাথে ডেট নিয়ে কাজ করা।
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);
}
আশা করি এটি সহায়ক।
এটি সহজ (এবং সম্ভবত বোকা :)) রাখা।
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.");
আমি কখনও খুঁজে পেয়েছি সবচেয়ে সহজ উপায় হ'ল এটি। এটি মার্কিন যুক্তরাষ্ট্র এবং পশ্চিমা ইউরোপের লোকালের জন্য সঠিকভাবে কাজ করে। অন্যান্য লোকেল, বিশেষত চীন এর মতো জায়গাগুলির সাথে কথা বলতে পারে না। 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 ;
}
আমি এর উত্তরগুলি সন্ধান করছিলাম এবং লক্ষ্য করেছি যে লিপ দিবসের জন্মের নিয়মিত / আইনী জালিয়াতির বিষয়ে কেউ উল্লেখ করেনি। উদাহরণস্বরূপ, উইকিপিডিয়া প্রতি , আপনি যদি ২৯ শে ফেব্রুয়ারী বিভিন্ন এখতিয়ারে জন্মে থাকেন তবে আপনি নন-লিপ বছরের জন্মদিনে ভিন্নতা থাকে:
এবং আমি যতটুকু বলতে পারি, মার্কিন যুক্তরাষ্ট্রে বিধিবিধিগুলি এ বিষয়ে সাধারণ নীতিতে এবং বিভিন্ন নিয়ন্ত্রক সংস্থা কীভাবে তাদের বিধিগুলিতে জিনিসগুলি সংজ্ঞায়িত করে তা সম্পর্কে নীরব থাকে।
সে লক্ষ্যে একটি উন্নতি:
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;
}
এই কোডটি ধরে নেওয়া উচিত:
TimeSpan diff = DateTime.Now - birthdayDateTime;
string age = String.Format("{0:%y} years, {0:%M} months, {0:%d}, days old", diff);
আপনি ঠিক কীভাবে আপনার কাছে ফিরে আসতে চান তা আমি নিশ্চিত নই, সুতরাং আমি কেবল একটি পঠনযোগ্য স্ট্রিং তৈরি করেছি।
এটি সরাসরি উত্তর নয়, তবে অর্ধ-বৈজ্ঞানিক দৃষ্টিকোণ থেকে হাতের সমস্যার বিষয়ে দার্শনিক যুক্তি বেশি।
আমি যুক্তি দিয়ে বলব যে প্রশ্নটি বয়স নির্ধারণের জন্য ইউনিট বা সংস্কৃতি নির্দিষ্ট করে না, বেশিরভাগ উত্তর একটি পূর্ণসংখ্যার বার্ষিক উপস্থাপনা বলে মনে হয়। সময়ের জন্য এসআই-ইউনিটটি হ'ল 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--;
ফিনান্সে একটি অনুরূপ সমস্যা থাকে যখন প্রায়শই ডে কাউন্ট ভগ্নাংশ হিসাবে উল্লেখ করা কিছু গণনা করা হয় যা নির্দিষ্ট সময়ের জন্য মোটামুটি কয়েক বছর a এবং বয়সের সমস্যাটি আসলে একটি সময় পরিমাপের সমস্যা।
প্রকৃত / প্রকৃত (সমস্ত দিন "সঠিকভাবে" গণনা করা) কনভেনশনের উদাহরণ:
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;
আমি অবাক হয়েছি যে আমাদের কতটা সময় ধরে আপেক্ষিক যুগে যুগে যুগে জীবনযাপনের সময় পৃথিবীর চারপাশে-সূর্য-চক্রের মোটামুটি পরিমাণের তুলনায় আরও কার্যকর হয়ে ওঠার আগে :) বা অন্য কথায়, যখন কোনও সময়কাল অবশ্যই একটি স্থান দেওয়া উচিত বা নিজের জন্য বৈধ হতে গতি উপস্থাপন একটি ফাংশন :)
এখানে একটি সমাধান।
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);
২৮ শে ফেব্রুয়ারির যে কোনও বছরের তুলনায় ২৯ শে ফেব্রুয়ারির জন্মদিনটি সমাধান করতে সক্ষম হ'ল এটি অন্যতম সঠিক উত্তর।
public int GetAge(DateTime birthDate)
{
int age = DateTime.Now.Year - birthDate.Year;
if (birthDate.DayOfYear > DateTime.Now.DayOfYear)
age--;
return age;
}
বয়স গণনা করার জন্য আমার কাছে একটি কাস্টমাইজড পদ্ধতি রয়েছে, প্লাস বোনাস বৈধতা বার্তাটি যদি সহায়তা করে তবে:
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
}
পদ্ধতিটি এখানে কল করুন এবং তারিখের মানটি পাস করুন (এমএম / ডিডি / ইয়াই যদি সার্ভারটি ইউএসএ লোকালে সেট করা থাকে)। এটি প্রদর্শন করতে কোনও বার্তা বাক্স বা কোনও ধারক দিয়ে এটিকে প্রতিস্থাপন করুন:
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
মনে রাখবেন আপনি যেভাবে চান বার্তাটি ফর্ম্যাট করতে পারেন।
কীভাবে এই সমাধান?
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"; ;
}
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;
}
নিম্নলিখিত পদ্ধতি (। নেট ক্লাস ডেটডিফের জন্য সময়সীমার পাঠাগার থেকে নিষ্কাশন ) সংস্কৃতি তথ্যের ক্যালেন্ডার বিবেচনা করে:
// ----------------------------------------------------------------------
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
এই ক্লাসিক প্রশ্নটি নোডা সময় সমাধানের উপযুক্ত।
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
পরামিতিও চান ।
এই বিষয়ে আমার ব্লগ পোস্টটিও দেখুন: জন্মদিনগুলি পরিচালনা এবং অন্যান্য বার্ষিকী
আমি কোনও ব্যক্তির বয়সের নিখুঁত বছরের গণনার জন্য 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;
}
মার্ক সোয়েনের উত্তরে আমি একটি ছোট্ট পরিবর্তন করেছি: আমি তৃতীয় লাইনটি আবারও লিখেছি যাতে এক্সপ্রেশনটি আরও সহজে পার্স করা যায়।
public int AgeInYears(DateTime bday)
{
DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday.AddYears(age) > now)
age--;
return age;
}
আমি স্বচ্ছতার স্বার্থে এটি একটি ফাংশনে পরিণত করেছি।