也可以用來判斷日期區遇到日期幾次
也許有更好的可以交流
public static int GetBetweenBirthdayCount(DateTime pFrom, DateTime pTo, DateTime pBirthday) { int mBirthdayCount = 0;//會過幾次生日 int TotalYear = pTo.Year - pFrom.Year;//看區間有幾年 for (int i = 0; i <= TotalYear; i++) { int y = pFrom.AddYears(i).Year; string m = pBirthday.Month.ToString().PadLeft(2, '0'); string d = pBirthday.Day.ToString().PadLeft(2, '0'); //如果他是生日229的要判斷 if (m == "02" && d == "29") {//四除的盡就算閏年 if (y % 4 != 0) { d = "28"; } } DateTime EveryBirthday = DateTime.Parse(y + "-" + m + "-" + d); if (pFrom <= EveryBirthday && pTo >= EveryBirthday) { mBirthdayCount++; } } return mBirthdayCount; }