我必须将当月的第一天作为开始日期发送,并将当天作为结束日期发送。我知道今天很热,但我不确定开始日期。
endDate = DateTime.Today.ToShortDateString();
但是我如何格式化开始日期以使其返回该月的第一天?
startdate = ??
我真的很感激任何帮助。
DateTime
从当前日期的年月创建一个新实例:
// Get currect date in a variable, to avoid repeated calls (as DateTime.Now changes)
DateTime today = DateTime.Today;
string startDate = new DateTime(today.Year, today.Month, 1).ToShortDateString();
string endDate = today.ToShortDateString();
DateTime
只需新建一个使用endDate
年份和月份值的实例,当天使用 1:
var startDate = new DateTime(endDate.Year, endDate.Month, 1);
startdate = endDate.AddDays(endDate.Day * -1 + 1);