将2次之间的差异转换为毫秒?

发布于 2024-08-06 21:56:15 字数 174 浏览 7 评论 0原文

我有两个屏蔽的 TextBox 控件,想知道如何获取每个控件中的时间,然后将差异转换为毫秒。比如,在 tb1 中我写“12:01”,在 tb2 中我写“12:02”,然后单击一个按钮。单击该按钮后,它会启动计时器,并在 12:02 时显示一个消息框。除了时间转换部分之外,我知道如何完成所有这些工作。

如何才能实现呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

倥絔 2024-08-13 21:56:15
DateTime dt1 = DateTime.Parse(maskedTextBox1.Text);
DateTime dt2 = DateTime.Parse(maskedTextBox2.Text);
TimeSpan span = dt2 - dt1;
int ms = (int)span.TotalMilliseconds;
DateTime dt1 = DateTime.Parse(maskedTextBox1.Text);
DateTime dt2 = DateTime.Parse(maskedTextBox2.Text);
TimeSpan span = dt2 - dt1;
int ms = (int)span.TotalMilliseconds;
你げ笑在眉眼 2024-08-13 21:56:15

回答标题问题:

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

int millisceonds = (int) diff.TotalMilliseconds;

您可以使用它来设置计时器:

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

处理刻度时不要忘记禁用计时器。

但如果您希望在 12:03 举办活动,只需将 d1 替换为 DateTime.Now 即可。

但目前还不清楚textBox1和textBox2的具体功能是什么。

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.

吃→可爱长大的 2024-08-13 21:56:15

您必须将文本框的值转换为 DateTime (t1,t2),然后:

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

或使用 DateTime.TryParse(textbox1, out t1);
错误处理由您决定。

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.

跨年 2024-08-13 21:56:15

上述许多解决方案可能适合不同的人。
我想建议一个比“MusiGenesis”最接受的解决方案稍作修改的代码。

DateTime firstTime = DateTime.Parse( TextBox1.Text );
DateTime secondTime = DateTime.Parse( TextBox2.Text );
double milDiff = secondTime.Subtract(firstTime).TotalMilliseconds;

注意事项
- earlierTime.Subtract(laterTime) 你会得到一个负值。
- 如果您需要整数值而不是双精度值,请使用 int milDiff = (int)DateTime.Now.Subtract(StartTime).TotalMilliseconds;
- 相同的代码可用于获取两个日期值之间的差异,您可能会得到 .TotalDays.TotalHours 而不是 .TotalMilliseconds

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".

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

椒妓 2024-08-13 21:56:15

如果您只处理时间而不处理日期,您将只想处理 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;

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;
ˉ厌 2024-08-13 21:56:15
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;
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;
浪漫之都 2024-08-13 21:56:15

请尝试以下操作:

   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;
   }

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;
   }
记忆之渊 2024-08-13 21:56:15

如果您只想在 12:02 显示消息框,请使用延迟为 250 毫秒的计时器控件,该控件会不断检查当前时间是否为 12:02。如果是,则显示消息并停止计时器。请注意,这不需要开始时间字段(尽管您可能将其用于其他用途 - 我不知道 - 在这种情况下,此处提供给您的其他代码将会有所帮助)。

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).

孤蝉 2024-08-13 21:56:15

尝试:

DateTime first;
DateTime second;

int milliSeconds = (int)((TimeSpan)(second - first)).TotalMilliseconds;

Try:

DateTime first;
DateTime second;

int milliSeconds = (int)((TimeSpan)(second - first)).TotalMilliseconds;
孤独难免 2024-08-13 21:56:15
    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;
    }
    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;
    }
丶视觉 2024-08-13 21:56:15

VB.net,桌面应用程序。如果您需要以毫秒为单位的经过时间:

Dim starts As Integer = My.Computer.Clock.TickCount
Dim ends As Integer = My.Computer.Clock.TickCount
Dim lapsed As Integer = ends - starts

VB.net, Desktop application. If you need lapsed time in milliseconds:

Dim starts As Integer = My.Computer.Clock.TickCount
Dim ends As Integer = My.Computer.Clock.TickCount
Dim lapsed As Integer = ends - starts
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文