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?
Using the general formula =B2-A2 will get an error result while the value in B2 is smaller than A2, here I introduce a formula to handle this.
There is also an elegant alternative to this solution: You can simply multiply the time value by 86400000 to convert it into milliseconds. This works because of the internal numeric format used by Excel where 1 is equal to 24h = 24h × 60min/h × 60s/min × 1000ms/s = 86400000ms.
If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.
To convert a second measurement to a millisecond measurement, multiply the time by the conversion ratio. The time in milliseconds is equal to the seconds multiplied by 1,000.
DateTime dt1 = DateTime.Parse(maskedTextBox1.Text); DateTime dt2 = DateTime.Parse(maskedTextBox2.Text); TimeSpan span = dt2 - dt1; int ms = (int)span.TotalMilliseconds;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With