1

我必须将当月的第一天作为开始日期发送,并将当天作为结束日期发送。我知道今天很热,但我不确定开始日期。

endDate = DateTime.Today.ToShortDateString();

但是我如何格式化开始日期以使其返回该月的第一天?

startdate = ??

我真的很感激任何帮助。

4

3 回答 3

4

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();
于 2012-05-19T20:28:52.897 回答
2

DateTime只需新建一个使用endDate年份和月份值的实例,当天使用 1:

var startDate = new DateTime(endDate.Year, endDate.Month, 1);
于 2012-05-19T20:28:16.137 回答
0

startdate = endDate.AddDays(endDate.Day * -1 + 1);

于 2012-05-19T20:28:14.260 回答