I’ve seen many examples of a simple Timer such as this
But what I need is to display the number of hours:minutes:seconds until a predetermined time, say 10pm every day.
Can anyone help? Looking for a C# solution.
I’ve seen many examples of a simple Timer such as this
But what I need is to display the number of hours:minutes:seconds until a predetermined time, say 10pm every day.
Can anyone help? Looking for a C# solution.
Do you mind clearing up what you would like to do? But apart from that I will give my solution to a countdown to 9pm. C# solution.
/* Import the system to the script */
using System; // if errors are still
/* The Code to get seconds left */
//Give the end time in digital format i.e. 21, 0, 0 == 9 pm
public int getSecondsLeft(int hours,int minutes ,int seconds) {
//Create Desired time
DateTime target = new DateTime(0,0,0,hours,minutes,seconds);
//Get the current time
DateTime now = System.DateTime.Now;
//Convert both to seconds
int targetSec = target.Hour*60*60 + target.Minute*60 + target.Second;
int nowSec = now.Hour * 60 * 60 + now.Minute * 60 + now.Second;
//Get the difference in seconds
int diff = targetSec - nowSec;
return diff;
}
Get current date and time using DateTime.Now add one day to it to get tomorrow and then subtract current date and time from the date of tomorrow with specific time (10pm) using DateTime.Subtract.
You can put the output in any format using the Custom Date and Time Format Strings
How to countdown to a specific date