C# 将两次之间的差异转换为毫秒?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1487146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Difference between 2 times into Milliseconds?
提问by jay_t55
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.
我有两个屏蔽的 TextBox 控件,想知道如何在每个控件中获取时间,然后将差异转换为毫秒。比如,在 tb1 中我写“12:01”,在 tb2 中我写“12:02”,然后单击一个按钮。单击按钮后,它将启动一个计时器,并在 12:02 显示一个消息框。除了时间转换部分,我知道如何做所有的事情。
How can it be achieved?
如何实现?
采纳答案by MusiGenesis
DateTime dt1 = DateTime.Parse(maskedTextBox1.Text);
DateTime dt2 = DateTime.Parse(maskedTextBox2.Text);
TimeSpan span = dt2 - dt1;
int ms = (int)span.TotalMilliseconds;
回答by Henk Holterman
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 at12:03, just substitute DateTime.Now for d1
.
但是,如果您想在12:03举办活动,只需将 DateTime.Now 替换为d1
.
But it is not clear what the exact function of textBox1 and textBox2 are.
但不清楚 textBox1 和 textBox2 的确切功能是什么。
回答by Viktor Jevdokimov
You have to convert textbox's values to DateTime (t1,t2), then:
您必须将文本框的值转换为 DateTime (t1,t2),然后:
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.
或者使用 DateTime.TryParse(textbox1, out t1); 错误处理由您决定。
回答by Frank Bollack
Try:
尝试:
DateTime first;
DateTime second;
int milliSeconds = (int)((TimeSpan)(second - first)).TotalMilliseconds;
回答by TLiebe
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;
}
回答by Jon Seigel
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).
如果您只想在 12:02 显示消息框,请使用延迟例如 250 毫秒的计时器控件,它会不断检查当前时间是否为 12:02。如果是,则显示消息并停止计时器。请注意,这不需要开始时间字段(尽管您可能将其用于其他用途——我不知道——在这种情况下,此处提供给您的其他代码会有所帮助)。
回答by DRBlaise
If you are only dealing with Times and no dates you will want to only deal with TimeSpan and handle crossing over midnight.
如果您只处理 Times 而没有日期,您将只想处理 TimeSpan 并处理跨越午夜。
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;
回答by Rakesh Nair Paruthikkat
var firstTime = DateTime.Now;
var secondTime = DateTime.Now.AddMilliseconds(600);
var diff = secondTime.Subtract(firstTime).Milliseconds;
// var diff = DateTime.Now.AddMilliseconds(600).Subtract(DateTime.Now).Milliseconds;
回答by Nashe
Many of the above mentioned solutions might suite different people.
I would like to suggest a slightly modified code than most accepted solution by "MusiGenesis".
许多上述解决方案可能适合不同的人。
我想建议比“MusiGenesis”大多数接受的解决方案稍微修改的代码。
DateTime firstTime = DateTime.Parse( TextBox1.Text );
DateTime secondTime = DateTime.Parse( TextBox2.Text );
double milDiff = secondTime.Subtract(firstTime).TotalMilliseconds;
Considerations:
- earlierTime.Subtract(laterTime)
you will get a negative value.
- use int milDiff = (int)DateTime.Now.Subtract(StartTime).TotalMilliseconds;
if you need integer value instead of double
- Same code can be used to get difference between two Date values and you may get .TotalDays
or .TotalHours
insteaf of .TotalMilliseconds
注意事项:
-earlierTime.Subtract(laterTime)
你会得到一个负值。
-int milDiff = (int)DateTime.Now.Subtract(StartTime).TotalMilliseconds;
如果您需要整数值而不是双精度
值,请使用 - 可以使用相同的代码来获取两个日期值之间的差异,您可能会得到.TotalDays
或.TotalHours
代替.TotalMilliseconds
回答by Er?in Dedeo?lu
public static Int64 GetDifferencesBetweenTwoDate(DateTime newDate, DateTime oldDate, string type)
{
var span = newDate - oldDate;
switch (type)
{
case "tt": return (int)span.Ticks;
case "ms": return (int)span.TotalMilliseconds;
case "ss": return (int)span.TotalSeconds;
case "mm": return (int)span.TotalMinutes;
case "hh": return (int)span.TotalHours;
case "dd": return (int)span.TotalDays;
}
return 0;
}