안녕하세요 저는 c#
을 처음 사용하고 프로그래밍하는 것은 일반적으로 다른 사람들의 코드를 수정하는 방법을 배우려 고 노력하고 있습니다. 아래 코드를 수정하면 금액을 계산하고 싶습니다. 1 월에 작업 일수가 남아 있지만 문제는 코드가 월의 일 단위로 실행되므로 이번 달에 29 일이지만 코드로 인한 코드 오류는 으로 실행됩니다. 나는 어떤 부분 어떤 도움을 변경할 수있는 코드의 것 큰 일C# 월에 남은 근무일을 계산하는 방법
private void days()
{
//Monday to Friday are business days.
var weekends = new DayOfWeek[] { DayOfWeek.Saturday, DayOfWeek.Sunday };
DateTime testing = DateTime.Now;
string month1 = testing.ToString("M ");
string year1 = testing.ToString("yyyy");
int month = Convert.ToInt32(month1);
int year = Convert.ToInt32(year1);
//Fetch the amount of days in your given month.
int daysInMonth = DateTime.DaysInMonth(year, month);
string daysleft = testing.ToString("d ");
int daystoday = Convert.ToInt32(daysleft);
//Here we create an enumerable from 1 to daysInMonth,
//and ask whether the DateTime object we create belongs to a weekend day,
//if it doesn't, add it to our IEnumerable<int> collection of days.
IEnumerable<int> businessDaysInMonth = Enumerable.Range(daystoday, daysInMonth)
.Where(d => !weekends.Contains(new DateTime(year, month, d).DayOfWeek));
//Pretty smooth.
int count = 0;
foreach (var day in businessDaysInMonth)
{
count = count + 1;
}
textBox9.Text = count.ToString();
}
}
가'자신을 GetDaysInMonth'. 프레임 워크는 이미'DateTime.DaysInMonth'을 포함하고 있습니다. 윤년 논리는 추악합니다 (프레임 워크 코드를 다시 복제합니다). 모듈러스 연산자를 사용하여 임의의 연도에 대한 도약을 계산할 수 있습니다. – CodesInChaos
나는 그것을하기위한 가장 효율적인 방법이 아니라 그것을하는 방법을 표현하기 위해 5 분 안에 그것을 썼다. – user1054326