1. 오늘 기준으로 표현하는 방법
// 오늘 날짜
DateTime today = DateTime.Now;
// 일주일 전 날짜
DateTime.Now.AddDays(-7);
// 전월 말일 구하기
New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
// 오늘 요일
DateTime.Today.DayOfWeek.ToString();
// 이번달 1일
DateTime.Today.AddDays(1 - DateTime.Today.Day);
// 이번달 마지막날짜
DateTime.Today.AddMonths(1).AddDays(DateTime.Today.Day)
2. 특정일을 지정하는 방법
// 특정날짜 지정하는 방법
new DateTime(2022, 1, 1);
// 3월 이전이면 전년도 3월부터 올해 3월까지
if (DateTime.Today.Month < 3)
{
int lastday = DateTime.DaysInMonth(DateTime.Today.Year, 2);
resvDt.FromDateTime = new DateTime(DateTime.Today.Year-1, 3, 1);
resvDt.ToDateTime = new DateTime(DateTime.Today.Year, 2, lastday);
}
// 3월 이후면 올해 3월부터 내년 3월까지
else
{
int lastday = DateTime.DaysInMonth(DateTime.Today.Year+1, 2);
resvDt.FromDateTime = new DateTime(DateTime.Today.Year, 3, 1);
resvDt.ToDateTime = new DateTime(DateTime.Today.Year+1, 2, lastday);
}반응형
'프로그래밍 > C#' 카테고리의 다른 글
| [C#] 람다 식 정리 (0) | 2022.06.24 |
|---|---|
| [C#] 테이블 중복된 Row 제거방법 (table.DefaultView.ToTable) (0) | 2022.06.24 |
| [C#] 그리드 관련 컨트롤 초기화 (0) | 2022.04.15 |
| [DevExpress] TabControl에서 Tab 추가하기 (0) | 2022.04.11 |
| [C#] devExpress 자주 쓰는 GridControl 정리 (0) | 2022.02.28 |