By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,783 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,783 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Feb 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 2 Feb 2007 08:46:45 -0800, "trint" <tr***********@gmail.comwrote:
>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
Feb 2 '07 #2

P: n/a
Mel
Look at the TimeSpan class

"trint" <tr***********@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>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

Feb 2 '07 #3

P: n/a
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 Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"trint" <tr***********@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>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

Feb 3 '07 #4

P: n/a
On Feb 3, 12:33 am, "Dave Sexton" <dave@jwa[remove.this]online.com>
wrote:
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)

"trint" <trinity.sm...@gmail.comwrote in message

news:11*********************@l53g2000cwa.googlegro ups.com...
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

Feb 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.