将倒计时日期计时器重置为另一个结束日期

Resetting a countdown to date timer to another date when it ends

好的,所以我想做的是倒计时何时结束,这意味着到达结束时间我已经把它放在 2 天内,我想重置并再添加 7 天,如果可能的话继续这样做。我试过使用

if(endTime.Subtract(DateTime.Now) = 0)
        {

        }

但这给了我一个错误 "The left-hand side of an assignment must be a variable, property or indexer" 并且还使用 .ToString() 方法将 ts 转换为字符串,但仍然无效!所有代码都取自 here 我想在那里发表评论,但我是新用户。提前致谢,我想我已经涵盖了所有内容,请在投票前提出任何要求!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TV_Series_New_Episodes
{
public partial class Flash : Form
{
    DateTime endTime = new DateTime(2015, 11 ,19, 14 ,30, 0);

    public Flash()
    {
        InitializeComponent();
    }

    private void ct_Tick(object sender, EventArgs e)
    {
        TimeSpan ts = endTime.Subtract(DateTime.Now);
        ctlb.Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
        if(endTime.Subtract(DateTime.Now) = 0)
        {

        }
    }

    private void Flash_Load(object sender, EventArgs e)
    {


        ct.Interval= 500;
        ct.Tick += new EventHandler(ct_Tick);
        TimeSpan ts = endTime.Subtract(DateTime.Now);
        ctlb.Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
        ct.Start();
    }
}
}

有两个问题:

= 是赋值运算符。如果要比较,用==.

您不能将 TimeSpanìnt 进行比较。使用 TimeSpanTotalDays 属性获取数字。或者直接使用参数。

最后,您的程序可能没有准确命中 endTime。所以你应该允许一些公差:

if(endTime <= DateTime.Now)
{

}

只是为了添加到 Nico 的回答中,endTime.Subtract(DateTime.Now) returns 一个具有 TotalTimeUnit 属性的 TimeSpan 对象(不是 DateTime)。