454,783 Members | 1,347 Online
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
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" I need to get the time from NOW and get how many milleseconds until1:00am for my countdown timer please.Any help is appreciated.Thanks,Trint Feb 2 '07 #2

 P: n/a Look at the TimeSpan class "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 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); } /// /// 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. /// 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" 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" 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); } /// /// 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. /// 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"

### This discussion thread is closed

Replies have been disabled for this discussion.