Need to calculate milleseconds until 1:00am...

  • Thread starter Thread starter trint
  • Start date Start date
T

trint

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
 
Get the current date and time. If time is before 1:00 am then target
time is current date plus 1 hour. If time is after 1:00 am then
target time is current date plus one day and one hour. Then subtract
current datetime from target datetime and get TotalMilliseconds.

HTH,

Sam
 
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;
}
 
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 -

THIS was the answer my man! I had such a mental block over this and
need the calculation for 'after 1:00' to get it reset for the next
'1:00am' (countdown done and reset for the next 1:00 am)! What can I
say but thanks!
Trint
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top