This article tells you a simple method to calculate time.
In fact, using TimeSpan can make the code easier to understand, and the code is very simple.
For example, I use the following code to represent 5 seconds
const int needCount = 5 * 1000;
Because the delay is used later, the delay code is very simple
(needCount)
At this time, a millisecond was passed in, but many friends asked why 5*1000 means 5 seconds. He didn't know that I was using milliseconds.
Therefore, it is recommended to use TimeSpan to write time. The following requirement is to judge the delay within 20 seconds of booting. If the application is started within 20 seconds of booting, then the delay time is required.
var needTime = (20); //Turn on20About seconds USB Loaded completed
You can use overload + and - to calculate the subtraction or addition of time. Please see the code below, which is to subtract two TimeSpans, and the returned value is also a TimeSpan. The code below does not compile and fails to pass.
var chikesereHearpawwirboo = needTime - maxDelay; (chikesereHearpawwirboo);
If you need to convert TimeSpan from milliseconds, please see the following code
// Milliseconds to TimeSpan var milliseconds = 5 * 1000; var time = (milliseconds); // TimeSpan milliseconds milliseconds =(int) ;
Because the value from seconds to milliseconds is double that needs to be converted, if you use int conversion, it will sometimes go beyond the bounds, it is recommended to use the following code
// Milliseconds to TimeSpan long milliseconds = 5 * 1000; var time = (milliseconds); // TimeSpan milliseconds milliseconds = (long) ();
This calculation is suitable for calculations with days and hours, such as calculating how many milliseconds are there for 1 day minus 3h10m. If you do not rewrite it yourself using TimeSpan, you still need to write a lot of code.
var time = (1); var cut = new TimeSpan(0, 3, 10, 0); var milliseconds = (long)(time - cut).TotalMilliseconds;
Try not to use TimeSpan and think about how to write
C# uses timespan and timer to complete a simple countdown timer
First, use the timespan data type to construct it like this:
TimeSpan ts = new TimeSpan(0, 45, 0);
This declares a time period of 45 minutes in which the constructor parameters mean:
TimeSpan(hour,minute,second);
Then drag a timer in, called timer1
=1000;
Set one cycle per second
In the timer's tick event, write it like this:
private void timer1_Tick(object sender, EventArgs e) { String str = () + ":" + () + ":" + (); = str;//label17 is used to display the remaining time ts = (new TimeSpan(0, 0, 1));//Subtract one second every second if ( < 0.0)//When the countdown is over { = false; ("The exam time is up, and the system will force the papers");//The prompt time is up, you can add the operation you want below } }
A simple countdown timer is completed like this. Hehe, I don’t know if I don’t look at it. It’s very simple to see it.
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.