简体   繁体   English

如何创建一个倒计时到给定时间和日期的计数器?

[英]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.

相关问题 如何在给定 UTC 时间和日期的情况下创建 DateTime 对象? - How do I create a DateTime object given UTC time and date? 如何使时间计数器从当前点而不是从开始继续向上/向下? - How can I make the time counter to continue up/down from the current point and not from the start? 如何使用日期格式创建这种格式的日期时间目录? - How can i create this format of date time directory but with my format? 如何获得给定日期和时间的 TimeSpan - How do I get TimeSpan given a date and a time 如何创建一个文件名,其中还包括第一天和最后一天的日期和时间? - How can i create a file name that will include also the date and time of the first day and the last day? 如何在 c# 中创建扩展以返回可以在 linq 中转换为实体的新日期时间条目? - How to I create an extension in c# to return a new date time entry that can be converted in linq to entity? 我如何检查日期时间的冲突 - How can i check conflict in date time 如何将日期和时间分开? - How can I split date and time separate? 如何计算/查找给定日期的周数? - How can I calculate/find the week-number of a given date? 如何创建在每个给定时间段运行一个函数的 BackGround 服务? 使用 C#(asp.net 核心 3.1.1) - How can I create a BackGround service that runs a function every given period of time ? Using C# (asp.net core 3.1.1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM