【问题标题】:C# Calculation current half yearC#计算当前半年
【发布时间】:2014-12-04 09:44:42
【问题描述】:

我想用c#计算从今天开始的最后半年。

在我的情景中,上半年是 01/01/ - 06/30/。 在我的情景中,下半年是 07/01/ - 12/31/。

你会如何使用 DateTime 来计算这个?

示例:今天是 03/15/2014 -> 上半年:07/01/2013 - 12/31/2013

示例:今天是 07/15/2014 -> 上半年:01/01/2014 - 06/30/2014。

提前致谢! ;-)

【问题讨论】:

  • 与now >= new DateTime(now.Year, 7, 1)比较
  • 应该是答案
  • 为什么今年是2014年最后半年是2013年?
  • @TimSchmelter 听起来 OP 想要完成半年。所以如果你使用DateTime.Today.AddMonths(-6),你的答案应该是正确的,就像 GvS 的更新答案一样。

标签: c# date datetime time


【解决方案1】:

您只需查看 6 个月前的月份,并确定它所在的一半:

DateTime start, end;

var date = DateTime.Today.AddMonths(-6);
var month = date.Month;
var year = date.Year;
if (month <= 6) {
    start = new DateTime(year, 1, 1);
    end = new DateTime(year, 6, 30);
} else {
    start = new DateTime(year, 7, 1);
    end = new DateTime(year, 12, 31);
}

【讨论】:

  • 我不会两次评估DateTime.Today。
【解决方案2】:

只需有一个 if else 来检查月份是否大于 6 并创建如下日期范围。

DateTime today = DateTime.Today;
if(today.Month > 6)
    Console.WriteLine(new DateTime(today.Year,1,1).ToShortDateString() + "->" + 
                      new DateTime(today.Year,6,30).ToShortDateString());
else
    Console.WriteLine(new DateTime(today.Year-1,6,1).ToShortDateString() + "->" + 
                      new DateTime(today.Year-1,12,31).ToShortDateString());

【讨论】:

    【解决方案3】:

    你可以这样做:

    var start = new DateTime(
        DateTime.Today.Year,
        1 + 6 * (DateTime.Today.Month / 7),
        1);
    var end = new DateTime(
        DateTime.Today.Year + DateTime.Today.Month / 7,
        7 - 6 * (DateTime.Today.Month / 7),
        1).AddDays(-1.0);
    

    【讨论】:

      【解决方案4】:

      后退一天,直到到达第 12 个月或第 06 个月

      // get first of this month
      DateTime prev =  new DAteTime( DateTime.Today.Year, DateTime.Today.Month, 1);
      
      bool found = false;
      while(found == false){
          prev = prev.AddDays(-1);
          if(prev.Month == 12)
              found = true;
          if(prev.Month == 6)
              found = true
      } 
      
      // then when found, count back 6 months from that
      
      DateTime start = prev.AddMonths(-6)
      
      // then get the first of that month
      start = new DateTime(start.Year, start.Month, 1);
      

      【讨论】:

      • 今天早上我的大脑仍然很模糊。 GvS 的答案要简单得多!
      【解决方案5】:

      请找到下面的sn-p。

                  string firstHalf = "30/06/";
      
                  int currentYear = DateTime.Now.Date.Year;
      
                  if (DateTime.Now.Date > Convert.ToDateTime(firstHalf + Convert.ToString(currentYear)).Date)
                  {
                      MessageBox.Show("Last Half Year : " + "01/01/" + Convert.ToString(currentYear) + " to  30/06/" + Convert.ToString(currentYear));
                  }
                  else
                      MessageBox.Show("Last Half Year : " + "01/07/" + Convert.ToString(currentYear - 1) + " to  31/12/" + Convert.ToString(currentYear - 1));
      

      【讨论】:

        【解决方案6】:

        谢谢你们。我更喜欢GvS的答案。

        我正在这样做:

                    var halfyear = (float)DateTime.Today.Month / 6;
                    var halfyearModulo = halfyear % 1;
        
                    if (halfyearModulo == 0)
                        halfyear--;
        
                    var minimumDate = new DateTime(DateTime.Today.Year, ((int)halfyear * 6) + 1, 1).AddMonths(-6);
                    var maximumDate = minimumDate.AddMonths(6);
        

        但我认为 GvSs anwser 更好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-29
          • 2011-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-14
          • 1970-01-01
          相关资源
          最近更新 更多