【发布时间】:2012-05-14 10:21:10
【问题描述】:
这是我的代码:
DateTime startDate = Convert.ToDateTime( orderDate ).AddMonths(-1);
DateTime endDate = Convert.ToDateTime( orderDate );
orderDate 是我输入的有效日期。
我如何始终保证 orderDate 的 startDate 是上个月的第一天? 我如何始终保证 endDate 是 orderDate 每月的最后一天?
例子:
orderDate = 5/4/2012
I want startDate to be 4/1/2012 (or 04/1/2012 whichever is default)
I want endDate to be 4/30/2012
我怎样才能做到这一点?
【问题讨论】:
-
为什么要将 DateTime 转换为 DateTime?
-
@TimeSchmelter 因为 orderDate 是从两个参数传入的字符串,可以通过文本框或查询字符串进行更改。
-
@JamesWilson:我刚刚问过,因为你说过 “orderDate 是我传入的有效日期”,而且确实有 an overload 需要一个 DateTime。
-
没有 4/31/2012 :)