Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to create a timer for a given time interval that should take an action when the limit is reached. For instance, if the time clocks 10.30 Am as it counts down from 9:25 am. (01:12.16) seconds remaining. It should not show the milliseconds.

What I have tried:

Void t_Tick(object sender, EventArgs e)
{Timespan tt = end.subtract(DateTIme.Now);
Tb.text = string. Format ({0} seconds left", TT);
}
Posted
Updated 21-Mar-19 20:58pm

See Date And Time Format In C#[^]

The following will not display milliseconds and can be adjusted to suit your date format requirements
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
 
Share this answer
 
Comments
Maciej Los 21-Mar-19 18:50pm    
5ed!
Robert S4r 22-Mar-19 0:40am    
This seems not to work for a countdown. It should count the remaining time to reach 10:30 second by second without showing milliseconds.
RossMW 22-Mar-19 1:50am    
I suspect you may have a problem with or how you are doing things. We can't guess your modified code and you need to provide the code and exactly what you are trying to achieve.
Solution #1 is very good and shows a way how to deal with date formats.

As to your code, check this:
C#
Void t_Tick(object sender, EventArgs e)
{
Timespan tt = end.Subtract(DateTIme.Now)
int sec = tt.Seconds;
Tb.text = string.Format({0} seconds left", sec);
} 


For further details, please see: TimeSpan.Seconds Property (System) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900