Date Time and Formula Improvement C# - c#

I got here a scenario where I input an information but the code does does not work. What happen is when I input 12/11/2015, the Address2Panel shows. Which is wrong because there no more date that I can input because Person A is born in 12/11/2015. The logic should Enter addresses for the past 5 years. But it goes wrong if the Birthdate gap is not lesser the 5 years from the current date.
Person A Birthday = 12/11/2015
Person A StartLiving = 12/11/2015 because its the day he/she was born.
Should not display Address2Panel
int CurrentDateInMonths = (((DateTime.Today.Year) * 12) + (DateTime.Today.Month));
static int AlienMonthsAtCurrentAddress = 0;
DateTime myDateTime;
//LivedHere = 12/11/2015
myDateTime = DateTime.Parse(LivedHere.Text);
AlienMonthsAtCurrentAddress = (CurrentDateInMonths - (((Convert.ToInt16(myDateTime.Year)) * 12) + Convert.ToInt16(myDateTime.Month)));
if (AlienMonthsAtCurrentAddress < 60)
{
Address2Panel.Visible = true;//shows the Address2Panel
}
else
{
ClearAddress2Panel();//hides also the Address2Panel
}
Any suggestion how should I improve my formula and date time manipulation?

no need to convert date into months, subtract dates using DateTime.Subtract method :
From MSDN, DateTime.Subtract Method subtracts the specified date and time from this instance.It returns a TimeSpan object which has a property Days
static int AlienMonthsAtCurrentAddress = 0;
try
{
DateTime myDateTime;
myDateTime = DateTime.Parse(LivedHere.Text);
// If you don't wish to subtract from today's date use required date in place of DateTime.Now
TimeSpan span = DateTime.Now.Subtract ( myDateTime );
if (span.Days < 60)
{
Address2Panel.Visible = true;//shows the Address2Panel
}
else
{
ClearAddress2Panel();//hides also the Address2Panel
}
}
catch { }

You could check years, months and days with a separately logic, and then get them all together:
DateTime date = new DateTime(2015,11,12)
DateTime input = getDate()
int years = input.Year - date.Year - 1
years += If(input.Month > date.Month, 1, 0)
years += If(input.Month = date.Month AndAlso input.Day >= date.Day, 1, 0)
This will output the exact number of years between two days (truncating the resulting integer). You'll just have to compare it to 5, in your case

Related

Simplest way to calculate anniversary given a date in the past?

i need a function that calculates if there is an upcoming anniversary for a person given a start date in the past and today's current date. I found this code below but looking at the below code and I have the feeling that there is a much simpler way to calculate this without required this for loop:
private const int ANNIVERSARY_ALERT = 10;
public virtual string UpcomingMilestone
{
get
{
var years = Years() + 2;
for (int year = 0; year < years; year++)
{
int days = year * 365;
int dayDiff = days - NumberOfDays;
if (dayDiff == 0)
{
return year + " year milestone";
}
if (dayDiff < ANNIVERSARY_ALERT && dayDiff > 0)
{
return year + " year milestone in " + dayDiff + " days";
}
}
return string.Empty;
}
}
public virtual int NumberOfDays
{
get
{
TimeSpan ts = DateTime.Today - StartDate.Value;
return (int)ts.TotalDays;
}
}
public virtual int Years()
{
TimeSpan span = DateTime.Now.Subtract(StartDate.Value);
return (int)(span.Days / 365.25); // leap years included
}
Can anyone suggest a way to calculate this above without having to do this loop? This is more for code maintainability versus any performance considerations.
Use the relevant parts of the startdate to compose a new DateTime:
//My birthday, feel free to put this date in your calendars!
var startDate = new DateTime(1976, 2, 29);
//Get the anniversary date for this year
DateTime nextAnniversary;
try
{
nextAnniversary = new DateTime(DateTime.Today.Year, startDate.Month, startDate.Day);
}
catch(ArgumentOutOfRangeException)
{
//DateTime conversion failed, try next day in the year
nextAnniversary = new DateTime(DateTime.Today.Year, startDate.AddDays(1).Month, startDate.AddDays(1).Day);
}
//Check if this year's anniversary has already happened
if(nextAnniversary < DateTime.Today) nextAnniversary = nextAnniversary.AddYears(1);
This should do it. You need to provide TimeSpan range that can be a day or a week, and date which you are matching anniversary to.
public bool Upcomming(DateTime date, TimeSpan range){
var newdate = new Date(DateTime.Now.Year, date.Month, date.Day);
return newdate - DateTime.Now < range;
}
int anniversaryYear = DateTime.Now.Year - StartDate.Year + 1;
DateTime nextAnniversary = StartDate.AddYears(anniversaryYear);
if (nextAnniversary == DateTime.Now)
{
return anniverasryYear + " year milestone";
}
if (DateTime.Now > nextAnniverasry)
{
anniverasryYear++;
nextAnniversary = StartDate.AddYears(anniversaryYear);
}
var daysTillNext = Math.Abs( (nextAnniversary - DateTime.Now).TotalDays );
return string.Format("{0} milestone in {1}", anniversaryYear, daysTillNext);
These methods should be sufficient for your purposes.
public DateTime GetNextAnniversaryDate(DateTime anniversary)
{
var today = DateTime.Today;
var year = anniversary.Month < today.Month ||
(anniversary.Month == today.Month && anniversary.Day < today.Day)
? today.Year + 1 : today.Year;
return anniversary.Month == 2 && anniversary.Day == 29 &&
!DateTime.IsLeapYear(year)
? new DateTime(year, 2, 28)
: new DateTime(year, anniversary.Month, anniversary.Day);
}
public int GetDaysUntilNextAnniversary(DateTime anniversary)
{
var nextDate = GetNextAnniversaryDate(anniversary);
return (int)(nextDate - DateTime.Today).TotalDays;
}
Note that we are specifically electing to celebrate leap-day anniversaries on Feb 28th when the anniversary year is not a leap year. You could change that to March 1 if desired.
Also note that this question assumes that the day of the person in question is the same day as the computer's local clock. This might not be true, for example if the local machine is in a time zone that is ahead of the user's time zone, then it might be one day off. If you want to take that into consideration, then you could use TimeZoneInfo and DateTime.UtcNow. Or you could use Noda Time. More about this on my blog.

How to calculate the number of years between 2 dates?

I would like to compare 2 dates to confirm that the number of years between is >= 18. For example, if my 2 dates are 03-12-2011 and 03-12-1983 then this should pass validation, however, if my 2 dates are 03-12-2011 and 03-12-1995 then this should fail validation.
Can anyone help me?
hope this is what you are looking for
public bool CheckDate(DateTime date1, DateTime date2)
{
return date1.AddYears(-18) < date2;
}
I re-jigged your question title & description to make it a bit more clear. From what I gathered from your original post you are looking for an Age Verification function. Here is what I would do:
function VerifyAge(DateTime dateOfBirth)
{
DateTime now = DateTime.Today;
int age = now.Year - dateOfBirth.Year;
if (now.Month < dateOfBirth.Month || (now.Month == dateOfBirth.Month && now.Day < dateOfBirth.Day))
age--;
return age >= 18;
}
Use TimeSpan structure.
TimeSpan span= dateSecond - dateFirst;
int days=span.Days;
//or
int years = (int) (span.Days / 365.25);
Check the TimeSpan structure:
http://msdn.microsoft.com/en-us/library/system.timespan.aspx
Use Timespan:
TimeSpan day = 03-12-2011 - 03-12-1983;
double year = day.TotalDays / 365.25;
if (year > 18)
{
}
Maybe instead of 03-12-2011 you should use DateTime.Now
Here's a method to check if the age is more than 18:
private bool IsMoreThan18(DateTime from, DateTime to)
{
int age = to.Year - from.Year;
if (from > to.AddYears(-age)) age--;
return age >= 18;
}
DateTime zeroTime = new DateTime(1, 1, 1);
DateTime a = new DateTime(2008, 1, 1);
DateTime b = new DateTime(2016, 1, 1);
TimeSpan span = b - a;
// because we start at year 1 for the Gregorian
// calendar, we must subtract a year here.
int years = (zeroTime + span).Year - 1;
Console.WriteLine("Years elapsed: " + years);
Refrence Link
Try this....
Using this you can get the exact no of years between two days. Only you need to do is divide the date difference by 365.25
TimeSpan span = DateTime.Parse(To_Date) - DateTime.Parse(From_Date);
int years = (int)(span.Days / 365.25);
Create two DateTime objects and substract them from eachother.
The result is a DateTime object aswel:
DateTime dt = new DateTime(2011, 12, 03);
DateTime dt2 = new DateTime(1983, 12, 03);
DateTime dt3 = dt - dt2;
Now you can check dt3.Year for the number of years between them

How to Compare DateTime C# Months and weeks

I need to compare a date in C#
if the date is less than 12 months,i need to set a boolean value
My Code is
String d = "26/06/10";
DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", null);
if ((dt > DateTime.Now.AddMonths(-12) ) )
{
Console.WriteLine("It is less than 12 months");
}
else
{
Console.WriteLine("It is more than 12 months");
}
Is the best way to compare date in c#.
similarly i need to compare date is less than two weeks or not
Any help appreciated
Thanks
sup
You could use TimeSpan to get the difference between two DateTime values
String d = "26/06/10";
DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", null);
DateTime dt2 = DateTime.Now.AddMonths(-12);
TimeSpan ts = dt - dt2;
You can use ts.Days to compare
You could do
DateTime date2 = DateTime.Now.AddMonths(-12);
//Or if you want to neglect the time part you could do
DateTime date2 = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0,0).AddMonths(-12);
String d = "26/06/10";
DateTime date1 = DateTime.ParseExact(d, "dd/MM/yy", null);
int result = DateTime.Compare(date1, date2);
string res;
if (result < 0)
Console.WriteLine("It is less than 12 months");
else if (result == 0)
res = "is the equal";
else
Console.WriteLine("It is more than 12 months");
The problem with your code snippet is that it will output "It is more than 12 months" even if the date is equal.
For two weeks:
if (dt1.Subtract(dt2).Days > 14)
{
...
}
For 12 Months(one year) (Considering day of the month is not important):
var monthDifference = ((dt1.Year - dt2.Year) * 12) + dt1.Month - dt2.Month
For a clearer understanding: you do not want to compare two dates (or DateTimes) but two TimeSpans. Namely the difference in time between now and the date you supplied - and the a time span of 12 months.
String d = "26/06/10";
DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", CultureInfo.InvariantCulture);
TimeSpan deltaTimeSpan = dt - DateTime.Now; // get the time difference between now and the time given
TimeSpan twelveMonths = new TimeSpan(365,0,0,0); // get a time span of 12 months
// round the amount of days down and always supply a positive number of days
int deltaTime = Convert.ToInt32(Math.Abs(Math.Floor(deltaTimeSpan.TotalDays)));
if (twelveMonths.TotalDays > deltaTime)
{
Console.WriteLine(string.Format("It is less than 12 months ({0} days).", deltaTime));
}
else if (twelveMonths.TotalDays < deltaTime)
{
Console.WriteLine(string.Format("It is more than 12 months ({0} days).", deltaTime));
}
else
{
Console.WriteLine(string.Format("The difference in time is exactly 12 months. ({0} days).", deltaTime);
}
Take note that this example certainly does not take in account leap years. The code does take in account weather the year to compare with lies in the past or the future (by converting the TimeSpan into a positive value and comparing against that one).
Adjusting the above code to do the same for two weeks or any other time span should be simple enough. Just change the TimeSpan I named "twelveMonths".
DateTime date1 = DateTime.Now.AddMonths(-12)
if(DateTime.Compare(dt, date1 )
{
//provided date is within 12 months
}
else
{
//provided date is after 12 months
}

Date difference in years using C# [duplicate]

This question already has answers here:
How do I calculate someone's age based on a DateTime type birthday?
(74 answers)
Closed 6 years ago.
How can I calculate date difference between two dates in years?
For example: (Datetime.Now.Today() - 11/03/2007) in years.
I have written an implementation that properly works with dates exactly one year apart.
However, it does not gracefully handle negative timespans, unlike the other algorithm. It also doesn't use its own date arithmetic, instead relying upon the standard library for that.
So without further ado, here is the code:
DateTime zeroTime = new DateTime(1, 1, 1);
DateTime a = new DateTime(2007, 1, 1);
DateTime b = new DateTime(2008, 1, 1);
TimeSpan span = b - a;
// Because we start at year 1 for the Gregorian
// calendar, we must subtract a year here.
int years = (zeroTime + span).Year - 1;
// 1, where my other algorithm resulted in 0.
Console.WriteLine("Yrs elapsed: " + years);
Use:
int Years(DateTime start, DateTime end)
{
return (end.Year - start.Year - 1) +
(((end.Month > start.Month) ||
((end.Month == start.Month) && (end.Day >= start.Day))) ? 1 : 0);
}
We had to code a check to establish if the difference between two dates, a start and end date was greater than 2 years.
Thanks to the tips above it was done as follows:
DateTime StartDate = Convert.ToDateTime("01/01/2012");
DateTime EndDate = Convert.ToDateTime("01/01/2014");
DateTime TwoYears = StartDate.AddYears(2);
if EndDate > TwoYears .....
If you need it for knowing someone's age for trivial reasons then Timespan is OK but if you need for calculating superannuation, long term deposits or anything else for financial, scientific or legal purposes then I'm afraid Timespan won't be accurate enough because Timespan assumes that every year has the same number of days, same # of hours and same # of seconds).
In reality the length of some years will vary (for different reasons that are outside the scope of this answer). To get around Timespan's limitation then you can mimic what Excel does which is:
public int GetDifferenceInYears(DateTime startDate, DateTime endDate)
{
//Excel documentation says "COMPLETE calendar years in between dates"
int years = endDate.Year - startDate.Year;
if (startDate.Month == endDate.Month &&// if the start month and the end month are the same
endDate.Day < startDate.Day// AND the end day is less than the start day
|| endDate.Month < startDate.Month)// OR if the end month is less than the start month
{
years--;
}
return years;
}
var totalYears =
(DateTime.Today - new DateTime(2007, 03, 11)).TotalDays
/ 365.2425;
Average days from Wikipedia/Leap_year.
int Age = new DateTime((DateTime.Now - BirthDateTime).Ticks).Year;
To calculate the elapsed years (age), the result will be minus one.
var timeSpan = DateTime.Now - birthDateTime;
int age = new DateTime(timeSpan.Ticks).Year - 1;
Here is a neat trick which lets the system deal with leap years automagically. It gives an accurate answer for all date combinations.
DateTime dt1 = new DateTime(1987, 9, 23, 13, 12, 12, 0);
DateTime dt2 = new DateTime(2007, 6, 15, 16, 25, 46, 0);
DateTime tmp = dt1;
int years = -1;
while (tmp < dt2)
{
years++;
tmp = tmp.AddYears(1);
}
Console.WriteLine("{0}", years);
It's unclear how you want to handle fractional years, but perhaps like this:
DateTime now = DateTime.Now;
DateTime origin = new DateTime(2007, 11, 3);
int calendar_years = now.Year - origin.Year;
int whole_years = calendar_years - ((now.AddYears(-calendar_years) >= origin)? 0: 1);
int another_method = calendar_years - ((now.Month - origin.Month) * 32 >= origin.Day - now.Day)? 0: 1);
I implemented an extension method to get the number of years between two dates, rounded by whole months.
/// <summary>
/// Gets the total number of years between two dates, rounded to whole months.
/// Examples:
/// 2011-12-14, 2012-12-15 returns 1.
/// 2011-12-14, 2012-12-14 returns 1.
/// 2011-12-14, 2012-12-13 returns 0,9167.
/// </summary>
/// <param name="start">
/// Stardate of time period
/// </param>
/// <param name="end">
/// Enddate of time period
/// </param>
/// <returns>
/// Total Years between the two days
/// </returns>
public static double DifferenceTotalYears(this DateTime start, DateTime end)
{
// Get difference in total months.
int months = ((end.Year - start.Year) * 12) + (end.Month - start.Month);
// substract 1 month if end month is not completed
if (end.Day < start.Day)
{
months--;
}
double totalyears = months / 12d;
return totalyears;
}
public string GetAgeText(DateTime birthDate)
{
const double ApproxDaysPerMonth = 30.4375;
const double ApproxDaysPerYear = 365.25;
int iDays = (DateTime.Now - birthDate).Days;
int iYear = (int)(iDays / ApproxDaysPerYear);
iDays -= (int)(iYear * ApproxDaysPerYear);
int iMonths = (int)(iDays / ApproxDaysPerMonth);
iDays -= (int)(iMonths * ApproxDaysPerMonth);
return string.Format("{0} år, {1} måneder, {2} dage", iYear, iMonths, iDays);
}
I found this at TimeSpan for years, months and days:
DateTime target_dob = THE_DOB;
DateTime true_age = DateTime.MinValue + ((TimeSpan)(DateTime.Now - target_dob )); // Minimum value as 1/1/1
int yr = true_age.Year - 1;
If you're dealing with months and years you need something that knows how many days each month has and which years are leap years.
Enter the Gregorian Calendar (and other culture-specific Calendar implementations).
While Calendar doesn't provide methods to directly calculate the difference between two points in time, it does have methods such as
DateTime AddWeeks(DateTime time, int weeks)
DateTime AddMonths(DateTime time, int months)
DateTime AddYears(DateTime time, int years)
DateTime musteriDogum = new DateTime(dogumYil, dogumAy, dogumGun);
int additionalDays = ((DateTime.Now.Year - dogumYil) / 4); //Count of the years with 366 days
int extraDays = additionalDays + ((DateTime.Now.Year % 4 == 0 || musteriDogum.Year % 4 == 0) ? 1 : 0); //We add 1 if this year or year inserted has 366 days
int yearsOld = ((DateTime.Now - musteriDogum).Days - extraDays ) / 365; // Now we extract these extra days from total days and we can divide to 365
Works perfect:
internal static int GetDifferenceInYears(DateTime startDate)
{
int finalResult = 0;
const int DaysInYear = 365;
DateTime endDate = DateTime.Now;
TimeSpan timeSpan = endDate - startDate;
if (timeSpan.TotalDays > 365)
{
finalResult = (int)Math.Round((timeSpan.TotalDays / DaysInYear), MidpointRounding.ToEven);
}
return finalResult;
}
Simple solution:
public int getYearDiff(DateTime startDate, DateTime endDate){
int y = Year(endDate) - Year(startDate);
int startMonth = Month(startDate);
int endMonth = Month(endDate);
if (endMonth < startMonth)
return y - 1;
if (endMonth > startMonth)
return y;
return (Day(endDate) < Day(startDate) ? y - 1 : y);
}
This is the best code to calculate year and month difference:
DateTime firstDate = DateTime.Parse("1/31/2019");
DateTime secondDate = DateTime.Parse("2/1/2016");
int totalYears = firstDate.Year - secondDate.Year;
int totalMonths = 0;
if (firstDate.Month > secondDate.Month)
totalMonths = firstDate.Month - secondDate.Month;
else if (firstDate.Month < secondDate.Month)
{
totalYears -= 1;
int monthDifference = secondDate.Month - firstDate.Month;
totalMonths = 12 - monthDifference;
}
if ((firstDate.Day - secondDate.Day) == 30)
{
totalMonths += 1;
if (totalMonths % 12 == 0)
{
totalYears += 1;
totalMonths = 0;
}
}
Maybe this will be helpful for answering the question: Count of days in given year,
new DateTime(anyDate.Year, 12, 31).DayOfYear //will include leap years too
Regarding DateTime.DayOfYear Property.
The following is based off Dana's simple code which produces the correct answer in most cases. But it did not take in to account less than a year between dates. So here is the code that I use to produce consistent results:
public static int DateDiffYears(DateTime startDate, DateTime endDate)
{
var yr = endDate.Year - startDate.Year - 1 +
(endDate.Month >= startDate.Month && endDate.Day >= startDate.Day ? 1 : 0);
return yr < 0 ? 0 : yr;
}

Determine if it has been 6 months since birthday in c#

My application needs to adjust a clients current age by +0.5 if it has been 6 months since their last birthday.
The code should look something like this, but how many ticks would there be in 6 months?
if (DateTime.Today - dateOfBirth.Date > new TimeSpan(6))
{
adjust = 0.5M;
}
else
{
adjust = 0M;
}
Thanks in advance
EDIT: You know what, actually? Since clearly what you really need is just to display the user's age to within 6 months, this is what you should really do.
static decimal GetApproximateAge(DateTime dateOfBirth) {
TimeSpan age = DateTime.Now - dateOfBirth;
/* a note on the line below:
* treating 182.5 days as equivalent to 6 months,
* reasoning that this will provide acceptable accuracy
* for ages below ~360 years
*
* (result will be 1 day off for roughly every 4 years;
* for a calculation of half-years to be inaccurate
* would take 4 [years] * ~90 [days] = ~360)
*/
// get age in units of 6 months
// desired behavior is not to add 0.5 until
// a full six months have elapsed since the user's last birthday --
// using Math.Floor to ensure this
double approxAgeInHalfYears = Math.Floor(age.TotalDays / 182.5);
// now convert this to years
// did it this way to restrict age to increments of 0.5
double approxAgeInYears = approxAgeInHalfYears * 0.5;
return Convert.ToDecimal(approxAgeInYears);
}
The above code includes a big comment explaining its own shortcomings, helpfully pointed out by David.
Now, here's yet another option. It's kind of ugly because it uses a loop; but it's also more rock-solid since it utilizes the DateTime.AddMonths method, which has the advantage of having already been tested and documented by Microsoft.
static decimal GetApproximateAge(DateTime dateOfBirth) {
DateTime now = DateTime.Now;
int birthYear = dateOfBirth.Year;
int lastYear = now.Year - 1;
// obviously, if the user's alive, he/she had a birthday last year;
// therefore he/she is at least this old
int minimumAgeInYears = lastYear - birthYear;
// now the question is: how much time has passed since then?
double actualAgeInYears = (double)minimumAgeInYears;
// for every six months that have elapsed since the user's birthday
// LAST year, add 0.5 to his/her age
DateTime birthDateLastYear = new DateTime(lastYear, 1, 1)
.AddDays(dateOfBirth.DayOfYear);
DateTime comparisonDate = birthDateLastYear
.AddMonths(6);
while (comparisonDate < now) {
actualAgeInYears += 0.5;
comparisonDate = comparisonDate.AddMonths(6);
}
return Convert.ToDecimal(actualAgeInYears);
}
This idea that people have been suggesting of checking dateOfBirth.AddMonths(6) is wrong. Since dateOfBirth is a DateTime, it represents the user's birth date, not their birth day.
What you want to check is if six months have elapsed since the user's last birthday--not the date they were born. Here's one way to do that:
DateTime lastBirthDay = GetLastBirthday(dateOfBirth);
if (DateTime.Today > lastBirthDay.AddMonths(6))
{
adjust = 0.5M;
}
else
{
adjust = 0M;
}
DateTime GetLastBirthday(DateTime dateOfBirth)
{
int currentYear = DateTime.Now.Year;
int birthMonth = dateOfBirth.Month;
int birthDay = dateOfBirth.Day;
// if user was born on Feb 29 and this year is NOT a leap year,
// we'll say the user's birthday this year falls on Feb 28
if (birthMonth == 2 && birthDay == 29 && !IsLeapYear(currentYear))
birthDay = 28;
DateTime birthdayThisYear = new DateTime(
currentYear,
birthMonth,
birthDay
);
if (DateTime.Today > birthdayThisYear)
return birthdayThisYear;
else
return birthdayThisYear.AddYears(-1);
}
bool IsLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
Why not if (dateOfBirth.Date.AddMonths(6) < DateTime.Today) instead?
long ticks = new DateTime(0).AddMonths(6).Ticks;
TimeSpan ts = new TimeSpan(ticks);
if (dateOfBirth.Date.AddMonths(6) < DateTime.Today)
{
age += 0.5;
}
I think you might be overcomplicating things:
DateTime displayDate = User.BirthDate; // User.BirthDate is a mock for however you get the birth date
if(DateTime.Now.AddMonths(-6) > displayDate)
{
// More than 6 Months have passed, so perform your logic to add .5 years
}
Something like this?
if (DateTime.Now.AddMonths(-6) > dateofBirth.Date)
{
dateOfBirth = dateOfBirth.AddMonths(6);
}
I think you actually want to know if it is half a year since their last birthday not six months (since months vary in length).
int daysDiff = DateTime.Now.DayOfYear - dayofBirth.DayOfYear;
if (daysDiff <0) daysDiff += 365;
double adjust = daysDiff > 365/2 ? 0.5 : 0.0;
DateTime today = DateTime.Today;
DateTime lastBirthday = dateOfBirth.Date.AddYears(today.Year - dateOfBirth.Year);
if (lastBirthday > today) lastBirthday = lastBirthday.AddYears(-1);
if (today > lastBirthday.AddMonths(6))
adjust = 0.5M;
else
adjust = 0M;

Categories