Created
May 14, 2014 20:49
-
-
Save achvaicer/aa17287ef08729629be1 to your computer and use it in GitHub Desktop.
Days 360 Excel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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