[英]How can I create a counter that counts down to a given time and date?
I have a given time and date and I want to create a counter in my game that is counting down to this given date.我有一个给定的时间和日期,我想在我的游戏中创建一个倒计时到这个给定日期的计数器。 I want to display the counter in this format: Days/Hours/Minutes/Seconds
我想以这种格式显示计数器:天/小时/分钟/秒
How can I convert the DateTime {2/3/2020 12:00:00 AM} to something like this: 0 Days / 9 Hours / 30 Minutes / 20 Seconds ?如何将 DateTime {2/3/2020 12:00:00 AM} 转换为这样的:0 天/9 小时/30 分钟/20 秒? The counter should run until it has reached the given time {2/3/2020 12:00:00 AM} (0 Days / 0 Hours / 0 Minutes / 0 Seconds).
计数器应一直运行,直到达到给定时间 {2/3/2020 12:00:00 AM}(0 天/0 小时/0 分钟/0 秒)。
I get the following date and time from the server but I don't know how to make a counter out of it.我从服务器获得以下日期和时间,但我不知道如何用它制作计数器。
How can I create a counter that counts down to a given time and date?如何创建一个倒计时到给定时间和日期的计数器?
var NextLeaderboardReset = resultleaderboard.Result.NextReset;
A DateTime
minus a DateTime
will give you a TimeSpan
object, which is what you want here.一个
DateTime
减去一个DateTime
会给你一个TimeSpan
对象,这就是你想要的。 Then you just need to get the correct string format for your countdown:然后你只需要为你的倒计时获取正确的字符串格式:
var countDownEnd = new DateTime(2020,2,3);
var timeSpan = DateTime.Now - countDownEnd;
var countDownString = $"Time left: {timeSpan.ToString(@"dd\:h\:m\:s")}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.