Skip to content

Instantly share code, notes, and snippets.

@achvaicer
Created May 14, 2014 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achvaicer/aa17287ef08729629be1 to your computer and use it in GitHub Desktop.
Save achvaicer/aa17287ef08729629be1 to your computer and use it in GitHub Desktop.
Days 360 Excel
internal abstract class AbstractDays360
{
public abstract int Calculate(DateTime start, DateTime end);
protected static int Calculate(DateTime start, DateTime end, int startDay, int endDay)
{
return (end.Year - start.Year) * 360 + (end.Month - start.Month) * 30 + (endDay - startDay);
}
protected static bool LastDayOfFebruary(DateTime date)
{
return date.Equals(new DateTime(date.Year,3, 1).AddDays(-1));
}
}
using System;
internal class USExcelCompatible : AbstractDays360
{
public override int Calculate(DateTime start, DateTime end)
{
var startDay = start.Day;
var endDay = end.Day;
if (startDay == 31 || LastDayOfFebruary(start))
startDay = 30;
if (startDay == 30 && endDay == 31)
endDay = 30;
return Calculate(start, end, startDay, endDay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment