如何在不同的变量中获取月份的开始日期和结束日期.我试过这个,但是我得到了开始日期但无法找到结束日期
DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd HH:mm:ss.fff");
DateTime endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(30).ToString("yyyy-MM-dd HH:mm:ss.fff");
Run Code Online (Sandbox Code Playgroud)
当一个月结束日期是31和28或29.您的帮助是肯定appretiated这个逻辑失败.
Ulu*_*rov 33
你可以endDate像这样计算:
DateTime endDate = startDate.AddMonths(1).AddDays(-1);
Run Code Online (Sandbox Code Playgroud)
Saj*_*ran 14
获得第一次约会
public DateTime FirstDayOfMonth(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1);
}
Run Code Online (Sandbox Code Playgroud)
获取最后日期
public DateTime LastDayOfMonth(DateTime dateTime)
{
DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34431 次 |
| 最近记录: |