简体   繁体   中英

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 ? 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).

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. 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")}";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM