views:

746

answers:

7

I have two masked TextBox controls and was wondering how I'd go about getting the time in each one and then converting the difference into milliseconds. Like, say in tb1 I write "12:01" and in tb2 I write "12:02", and then click a button. Once the button's clicked it starts a timer and at 12:02 a messagebox will be displayed. I know how to do all of it except for the time conversion part.

How can it be achieved?

+4  A: 

To answer the title-question:

DateTime d1 = ...;
DateTime d2 = ...;
TimeSpan diff = d2 - d1;

int millisceonds = (int) diff.TotalMilliseconds;

You can use this to set a Timer:

timer1.interval = millisceonds;
timer1.Enabled = true;

Don't forget to disable the timer when handling the tick.

But if you want an event at 12:03, just substitute DateTime.Now for d1.

But it is not clear what the exact function of textBox1 and textBox2 are.

Henk Holterman
You need to use TotalMilliseconds, not Milliseconds.
MusiGenesis
You need "TotalMilliseconds" not "Milliseconds"Milliseconds will only give you the millisecond "remainder" of the time difference, not the entire time span.2.475 seconds gives Milliseconds=475, TotalMilliseconds=2475
StarPacker
Yes, corrected. Darn TimeSpan interface.
Henk Holterman
+1  A: 

You have to convert textbox's values to DateTime (t1,t2), then:

DateTime t1,t2;
t1 = DateTime.Parse(textbox1.Text);
t2 = DateTime.Parse(textbox2.Text);
int diff = ((TimeSpan)(t2 - t1)).TotalMilliseconds;

Or use DateTime.TryParse(textbox1, out t1); Error handling is up to you.

Viktor Jevdokimov
Writing from brain memory :)))
Viktor Jevdokimov
You can still loose the typecast.
Henk Holterman
You were right the first time: you need to use TotalMilliseconds (unless you like returning 0 every time).
MusiGenesis
A: 

Try:

DateTime first;
DateTime second;

int milliSeconds = (int)((TimeSpan)(second - first)).TotalMilliseconds;
Frank Bollack
No need to type-cast to TimeSpan, and TotalMilliseconds is a double.
Henk Holterman
The cast does'n hurt performance and makes the intention clearer. The cast of the return value to int also makes sense, as it has to be casted anyway to use it with the timer, as the OP wants to. By the way, the code is meant as an example, so if there is anything wrong and worth a down vote, please elaborate.
Frank Bollack
There is no cast of the return value. The last line won't compile.
Henk Holterman
A: 

Try the following:

   DateTime dtStart;
   DateTime dtEnd;

   if (DateTime.TryParse( tb1.Text, out dtStart ) && DateTime.TryParse(tb2.Text, out dtEnd ))
   {
      TimeSpan ts = dtStart - dtEnd;
      double difference = ts.TotalMilliseconds;
   }
TLiebe
A: 

If you just want to display a message box at 12:02, use a timer control with delay of, say, 250ms, that keeps checking if the current time is 12:02. When it is, display the message and stop the timer. Note this does not require a start time field (although you may be using that for something else -- I don't know -- in which case the other code provided to you here will be helpful).

Jon Seigel
+1  A: 
DateTime dt1 = DateTime.Parse(maskedTextBox1.Text);
DateTime dt2 = DateTime.Parse(maskedTextBox2.Text);
TimeSpan span = dt2 - dt1;
int ms = (int)span.TotalMilliseconds;
MusiGenesis
A: 

If you are only dealing with Times and no dates you will want to only deal with TimeSpan and handle crossing over midnight.

TimeSpan time1 = ...;  // assume TimeOfDay
TimeSpan time2 = ...;  // assume TimeOfDay
TimeSpan diffTime = time2 - time1;
if (time2 < time1)  // crosses over midnight
    diffTime += TimeSpan.FromTicks(TimeSpan.TicksPerDay);
int totalMilliSeconds = (int)diffTime.TotalMilliseconds;
DRBlaise