获取月份和年份的日期时间

时间:2015-05-13 21:47:31

标签: c#

我试图获得该月的第一个月到最后两天的月份范围(-2)

例如:2015年5月1日或2015年1月5日。

我想将最后两天作为2015年4月29日结束日期和该月的第一天(2015年4月1日)。

所以我的范围是04/01/15 - 2015年4月29日,当天是05/01/2015。

因此,如果当天是05/02/15,我希望获得04/01/15 - 04/30/15的范围。

但是一旦它到达05/03/15,它就会回到05/01/15。

所以这就是我到目前为止:

    static DateTime GetMonthly = DateTime.Today.AddDays(-2);

    static string StartMonthly = ?????

    static string EndMonthly = GetMonthly.ToString("yyyy-MM-dd");

我知道如何获得EndMonthly但不是GetMontly -2天的第一天。

另外,我需要根据GetMonthly获取年份。因此,当它2016年1月1日,我想要抓住12/01/15 - 12/30/15

有谁知道我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我知道有更好的方法可以做到,但我最终做到了这一点:

static DateTime GetMonthly = DateTime.Today.AddDays(-2);

static string StartMonthly = GetMonthly.Year + "/" + GetMonthly.Month.ToString().PadLeft(2, '0') + "/" + "01";

static string EndMonthly = GetMonthly.ToString("yyyy-MM-dd");

只想展示再次提出这个问题的人。