1

我有一个程序,我想为“每月的第一天”分配一个变量。这是我到目前为止所拥有的:

Dim dtToday As DateTime = DateTime.Today 'Sets todays date (04/15/13)
Dim dtYesterday As DateTime = DateTime.Today.AddDays(-1) 'Sets Yesterdays Date (04/14/13)

我想接下来定义“dtFirstOfMonth”。它将每月的第一天设置为“04/01/13”这将根据它运行的日期而改变......

4

3 回答 3

4
Dim dtFirstOfMonth as DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
于 2013-04-15T19:22:34.803 回答
2

您可以从当前日期减去当前日期。然后添加1以获取第一天:

var firstDayOfMonth = DateTime.Today.AddDays(1 - DateTime.Today.Day);
于 2013-04-15T19:26:23.623 回答
0

试试下面的方法,它会帮助你...

 Dim dtToday As DateTime = DateTime.Today
 Dim dtFrom As DateTime
 dtFrom = dtToday.AddDays(-(dtToday.Day - 1))
于 2013-04-15T19:26:27.903 回答