C#时间处理(DateTime和TimeSpan)
具体使用方法如下:
//取得某月的最后一天
//方法一:使用算出該月多少天,年+月+加上多少天即得,舉例取今天這個月的最后一天
private void GetLastDateForMonth(DateTime DtStart, out DateTime DtEnd)
{
int Dtyear, DtMonth;
DtStart = DateTime.Now;
Dtyear = DtStart.Year;
DtMonth = DtStart.Month;
int MonthCount = DateTime.DaysInMonth(Dtyear, DtMonth);//計算該月有多少天
DtEnd = Convert.ToDateTime(Dtyear.ToString() + "-" + DtMonth.ToString() + "-" + MonthCount);
}
//方法二:取出下月的第一天減去一天便是這個月的最后一天
private void GetLastDateForMonth(DateTime DtStart, out DateTime DtEnd)
{
int Dtyear, DtMonth;
DtStart = DateTime.Now.AddMonths(1);//月份加1
Dtyear = DtStart.Year;
DtMonth = DtStart.Month;