【问题标题】:TimeSpan countdown between given dates给定日期之间的 TimeSpan 倒计时
【发布时间】:2015-08-31 23:22:16
【问题描述】:

我会在两个给定日期之间计算TimeSpan,倒计时到 0 天。我有以下代码:

partial void mExpiration_Compute(ref string result)
    {

        DateTime a = mExpiryDate.AddMonths(-1);
        DateTime b = mExpiryDate;

        TimeSpan ts = b.Subtract(a);

        // Difference in days.
        int c = ts.Days;

        result = Convert.ToString(c);

    }

如果TimeSpan 当前是 31,那么我想要减少平衡时间。示例:“您还有 24 天的到期日。”注意:余额减少是(31、30、..、24等)

如何在给定日期之间倒计时。

【问题讨论】:

  • 它在 Visual Studio 2015 中给了我正确的 31 天......我为 DateTime.Now 设置的 mExpiryDate 的值是多少
  • mExpiryDate 的值是 02/09/215 减去 mExpiryDate.AddMonths(-1) of 02/08/2015
  • 我确信上面的代码可以正常工作并为我返回 31 天。我建议发布其余代码。因为这对我来说按预期工作
  • 我同意你的观点,代码有效。问题是我会倒计时到 0 天,31 天减去每一天到 0 天
  • 我真的不明白你需要什么作为输出!!你能发布你期望的输出吗??

标签: c# visual-studio-lightswitch lightswitch-2012


【解决方案1】:

您的函数永远不会返回 31 以外的任何值,因为您正在计算相对于到期日期正好一个月剩余的天数(即函数 always 计算 02/09/21502/08/2015 之间的天数,因此总是返回一个常数值)。

假设您的 mExpiryDate 保持不变并且不会更改,这就是我计算剩余天数的方式(相对于当前日期):

TimeSpan ts = mExpiryDate.Subtract(DateTime.Now);
return ts.Days;

【讨论】:

  • 在触发通知时必须指定 mExpiryDate.AddMonths(-1)(“您还有 24 天的到期日)。DateTime.Now 不是到期期间之间的正确时间。
  • 因此,听起来您需要额外的逻辑来仅在天数小于零时才设置“通知”。重申一下,如果 expiryDate 没有改变,那么您的函数将始终返回 31。
【解决方案2】:

在不知道表单逻辑的情况下,我想问题是是否存在到期日期。

如果存在,则检索它;如果没有,请从今天开始创建并添加一个月。

然后计算到期天数。

它会像这样(独立代码,根据您的应用程序调整):

// Choose either setting 1. or 2. for mExpiryDate.
// Here setting 1. is active:

//      1. Simulate expiry date already set on August 17th.
DateTime mExpiryDate = new DateTime(2015, 9, 17);

//      2. Expiry date not set.
//DateTime mExpiryDate = DateTime.Today.AddMonths(1);

TimeSpan ts = mExpiryDate.Subtract(DateTime.Today);

// Difference in days.
int c = ts.Days;

string result = Convert.ToString(c);

这将在 2015 年 9 月 1 日这一天,返回 16。

【讨论】:

    【解决方案3】:

    我认为您缺少一个参数。这是你需要的吗:

    static DateTime mExpiryDate = DateTime.Parse("09/29/2015");
    public static void Main()
    {
        var result = string.Empty;
        var noticeDate = DateTime.Parse("09/05/2015");
        mExpiration_Compute(ref result, noticeDate);
        Console.WriteLine(result);
    }
    
    public static void mExpiration_Compute(ref string result, DateTime noticeDate)
    {
        DateTime a = noticeDate;
        DateTime b = mExpiryDate;
        TimeSpan ts = b.Subtract(a);
        result = string.Format("You have {0} days to expiry date.", ts.Days);
    }
    

    https://dotnetfiddle.net/9mDPx2

    【讨论】:

    • 是的,但输出不是循环条件。它反映了一个日期。
    • 好的,所以你有一个日期 mExpiryDate,你需要倒计时到另一个日期。我在您的代码中没有看到其他日期。现在日期 a 和日期 b 都基于 mExpiryDate,因此您的差值将始终为 31。您要从 mExpiryDate 倒计时到当前日期吗?
    【解决方案4】:

    这是给定日期之间倒计时的解决方案。

        partial void mExpiration_Compute(ref string result)
        {
    
            DateTime expStart = mExpiryDate.AddMonths(-1);
            DateTime expDate = mExpiryDate;
            DateTime currentDate = DateTime.Now;
    
            // Difference in days, hours, and minutes.
            TimeSpan expSpanGivenDates = expDate.Subtract(expStart);
            TimeSpan expSpanDateNow = expDate.Subtract(currentDate);
    
            // Difference in days
            int numGivenDays = expSpanGivenDates.Days;
            int numNowDays = expSpanDateNow.Days;
            int calculateDays = numGivenDays - (numGivenDays - numNowDays);
    
            int days = 0;
    
            if (calculateDays >= 0 && calculateDays < 31)
            {
                days = calculateDays;
            }
    
            result = Convert.ToString(days);
    
        }
    

    它将返回两个给定日期之间的剩余天数。有效期前后均为 0。

    输出将是:0 0 31 24 8 9 0 0 0 .....

    【讨论】:

      【解决方案5】:

      这很好用!

          public TimeSpan ElapsedTimeFormatted
          {
              get
              {
                  if (FinishedOn != null &&
                      StartedAt != null)
                  {
                      TimeSpan durationCount = new TimeSpan();
      
                      int hours = 0;
                      int minutes = 0;
                      int seconds = 0;
      
                      var times = Segments.Select(c => c.ElapsedTimeFormatted).ToList();
      
                      foreach (var time in times)
                      {
                          TimeSpan timeParse = TimeSpan.Parse(time);
      
                          hours = hours + (int)timeParse.Hours;
                          minutes = minutes + (int)timeParse.Minutes;
                          seconds = seconds + (int)timeParse.Seconds;
      
                          durationCount = new TimeSpan(hours, minutes, seconds);
                      }
      
                      return durationCount;
                  }
      
                  return new TimeSpan();
              }
          }
      

      【讨论】:

      • 请不要只在答案中添加代码。解释它的作用,以便其他人可以从您的回答中学习。
      • 顺便说一句。您的示例无法正常工作,因为所有变量都丢失了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多