倒数计时器使用DateTime.Parse没有定义日期

时间:2017-02-24 21:21:49

标签: c# datetime

我正在写一个机器人,它会在每周的特定日期和时间倒计时。在这种情况下,我们今天会说星期五。我希望倒计时到星期六8点。我目前的格式问题是它有一个确定的日期。如果没有DateTime.Parse的明确日期,我怎样才能让它在每个星期六都有效?如果我不这样做,那么什么是好的选择?

        DateTime dt;
        TimeSpan t;
        string countDown = "";

        dt = DateTime.Now;
        dt.DayOfWeek.ToString();

        if (dt.DayOfWeek.ToString() == "Friday")
        {
            //Get these values however you like.
            DateTime productLaunchDateTime = DateTime.Parse("02/25/2017 08:00:00 PM");
            DateTime startDate = DateTime.Now;

            //Calculate countdown timer.
            t = productLaunchDateTime - startDate;
            countDown = string.Format("Release in {0} Days, {1} Hours, {2} Minutes, {3} Seconds", t.Days, t.Hours, t.Minutes, t.Seconds);

        }

1 个答案:

答案 0 :(得分:1)

你可以查看这篇文章。 Quartz就是你要找的。您还可以检查任务调度程序以获得简单的解决方案。

How to execute code at a given time every day?