Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
Here's a JavaScript example: https://codepen.io/anon/pen/wexJxV
As you can see on line 17, 60s was being divided into 59s. How can I achieve this in C#?

What I have tried:

C#
public Form1()
{
    Timer t = new Timer();
    t.Interval = 1;
    t.Tick += countdown;
    t.Start();


}

private void countdown(object sender, EventArgs e)
{
    DateTime end = new DateTime(2017, 12, 14);
    DateTime now = DateTime.Now;

    TimeSpan diff = end - now;
    string diff = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds", diff.Days, diff.Hours, diff.Minutes, diff.Seconds);
}
Posted
Updated 6-Jul-17 0:53am
Comments
Suvendu Shekhar Giri 6-Jul-17 6:14am    
What is the issue with the tried code?
Richard Deeming 6-Jul-17 8:56am    
The CodePen you've linked to has no code in it. It looks like someone just let their cat walk over the keyboard.

Click "Improve question" and add the correct link.

1 solution

Okay, what you want is for the seconds to not be seconds but 1/59 fractions of a minute.
I don't know why you're not happy with the above that does it in exact seconds.
But say you want what you asked for the instead of diff.seconds you'd have to put in something like:

Math.Floor((diff.TotalMiliseconds % (1000*59))/1000);


But why you'd want this is beyond me. Try with 360000 (or 6 hours clean)
(360000 Mod 59000)/1000 = 6. So there is 6 seconds there out of nowhere!

The more time elapsing the worse this will be.

Included the code snippet you were referring to:
// Time calculations for days, hours, minutes and seconds
   var days = Math.floor(distance / (1000 * 60 * 60 * 24));
   var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
   var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
   var seconds = Math.floor((distance % (1000 * 59)) / 1000);
 
Share this answer
 
v3

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