2009-11-24 4 views
0

나는이 코드 조각이있다. 하지만 월요일에 계산해서 일주일 만에 계산하길 바래. 왜냐하면 2009-11-29, 2009-11-30로 돌아 가면 2009-11-23 대신에 다시 돌아 오기 때문입니다.계산 날짜 = 1

어떻게 코드를 조정할 수 있습니까? 다음, 나는이 일을해야한다고 생각

public static DateTime calcMondayDate(DateTime input) { 
    int delta = (DayOfWeek.Monday - input.DayOfWeek - 7) % 7; 
    DateTime monday = input.AddDays(delta); 
    return monday; 
} 

답변

2

이 하나의 시도

public static DateTime calcMondayDate(DateTime input) 
{ 
    int delta = (7 - (DayOfWeek.Monday - input.DayOfWeek)) % 7; 
    return input.AddDays(-delta); 
} 
0

이전 월요일 원하는 가정 (월요일이있는 경우 또는 같은 날) :

/M

0
DateTime temp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 7, 0, 0); 
starting=temp.AddDays(-(int) temp.AddDays(-1).DayOfWeek); 

이 작품은