Hi,
Here's a solution with three test cases:
static void Main(string[] args)
{
// before 1:00AM
DateTime now = DateTime.Parse("12:30AM");
Console.WriteLine("Test 1: {0}", now);
Console.WriteLine();
double millisecondsTo1AM = GetMillisecondsTo1AM(now);
Console.WriteLine("Milliseconds: {0}", millisecondsTo1AM);
Console.WriteLine("Hours: {0}", millisecondsTo1AM /
1000 / 60 / 60);
Console.WriteLine();
// at 1:00AM
now = DateTime.Parse("1:00AM");
Console.WriteLine("Test 2: {0}", now);
Console.WriteLine();
millisecondsTo1AM = GetMillisecondsTo1AM(now);
Console.WriteLine("Milliseconds: {0}", millisecondsTo1AM);
Console.WriteLine("Hours: {0}", millisecondsTo1AM /
1000 / 60 / 60);
Console.WriteLine();
// after 1:00AM
now = DateTime.Parse("2:30PM");
Console.WriteLine("Test 3: {0}", now);
Console.WriteLine();
millisecondsTo1AM = GetMillisecondsTo1AM(now);
Console.WriteLine("Milliseconds: {0}", millisecondsTo1AM);
Console.WriteLine("Hours: {0}", millisecondsTo1AM /
1000 / 60 / 60);
}
/// <summary>
/// Gets the number of milliseconds from the specified time
/// to 1:00 AM today, or tomorrow if 1:00 AM has already
/// passed for today. If the specified time is 1:00 AM then
/// 24 hours is returned, in milliseconds.
/// </summary>
static double GetMillisecondsTo1AM(DateTime from)
{
DateTime one = DateTime.Parse("1:00AM",
new System.Globalization.CultureInfo("en-US"));
return (((from < one)
? one : one.AddHours(24)) - from).TotalMilliseconds;
}
--
Dave Sextonhttp://davesexton.com/bloghttp://
www.codeplex.com/DocProject(Sandcastle in VS IDE)
I need to get the time from NOW and get how many milleseconds until
1:00am for my countdown timer please.
Any help is appreciated.
Thanks,
Trint- Hide quoted text -
- Show quoted text -