如何在VB中计算两次之间的时差 [英] How to calculate time difference between two times in VB

查看:146
本文介绍了如何在VB中计算两次之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算两次之间的时差

班次开始时间是04:30:Pm到12:30

员工入职时间是08:30 AM然后它显示错误消息时间已过期



我尝试过:



 dtShifTime = Convert.ToDateTime(  16:30 ).ToString(  HH:mm
Dim dtEntryTime As DateTime = Convert.ToDateTime( 08:30)。ToString( HH:mm
dtLateTime = dtShifTime.AddHours( 2
如果 lblStartTime.Text< > (dtShifTime< dtEntryTime )然后
MBox( Time Expired
结束 IF

解决方案

您是否尝试过询问Google?

https://social.msdn.microsoft.com/Forums/en -US / b196af37-980f-4312-a318-6119da34d60e / time-difference-in-vbnet?forum = vblanguage


问题是你的班次重叠了两天,在第二天半小时。

因此,当您将时间转换为DateTime对象时,您将获得结束时间之后的开始时间:00:30结束,16:30开始。



然后是字符串比较......为什么?为什么要将字符串转换为数字然后返回字符串以进行数字比较?



相反,请将其视为两班:00:00至00:30 16:30到23:59并比较两者。

最好的方法是将字符串作为午夜分钟开始:

0到30 ,990到1439并将当前时间转换为相同的东西:510。

然后检查它是否处于两个班次中的任何一个。



顺便说一句:08:30不在你的班次......所以时间过期会是正确的......


你的问题是你设置16 :30变为 dtShifTime ,然后在比较时使用 dtLateTime

dtLateTime 未在此代码中定义。

使用调试器在运行时查看变量的值。



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个almos通用解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它不知道你的cpde应该做什么,它没有找到bug,它只是通过向你显示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



Visual Basic / Visual Studio Video教程 - 基本调试 - YouTube [ ^ ]

初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]



在Excel VBA中调试 - 轻松的Excel宏 [ ^ ]

MS Excel 2013:VBA调试简介 [ ^ ]

如何调试Excel VBA - YouTube [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。

引用:

我已经使用调试器查找代码如何工作但我不知道哪个条件检查时间差,因为应用了一些条件



由于你只工作几小时,你在当天有一套条件,而当你在轮班中改变一天时有另一条条件。 / BLOCKQUOTE>

I need to calculate time difference between two time
The shift Start time is 04:30:Pm to 12:30AM
The Employee IN time is 08:30 AM then it shows the error message time expired

What I have tried:

dtShifTime = Convert.ToDateTime("16:30").ToString("HH:mm")
   Dim dtEntryTime As DateTime = Convert.ToDateTime("08:30").ToString("HH:mm")
dtLateTime = dtShifTime.AddHours(2)
     If lblStartTime.Text <> "" And (dtShifTime  < dtEntryTime) Then
                    MBox("Time Expired")
End IF

解决方案

Have you tried to ask Google ?
https://social.msdn.microsoft.com/Forums/en-US/b196af37-980f-4312-a318-6119da34d60e/time-difference-in-vbnet?forum=vblanguage


The problem is that your shift overlaps two days, with half an hour in the "second day".
so when you just convert a time to a DateTime object, you get the start time after the end time: 00:30 end, 16:30 start.

Then there is the string comparison ... why? Why convert strings to numbers and then back to strings in order to do a numeric comparison?

Instead, consider it as two shifts: 00:00 to 00:30 and 16:30 to 23:59 and compare for both.
The best way to do this is to consider the string starts as "minutes since midnight":
0 to 30, 990 to 1439 and convert the current time to the same thing: 510.
Then check if it is in either of the two shifts or not.

By the way: 08:30 is not in your shift at all ... so "time expired" would be correct...


Your problem is that you set 16:30 to variable dtShifTime, and then you use dtLateTime when comparing.
dtLateTime is not defined in this code.
Use the debugger to see the values of variables at runtime.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

Debugging in Excel VBA - EASY Excel Macros[^]
MS Excel 2013: VBA Debugging Introduction[^]
How to debug Excel VBA - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

Quote:

I'm already using debugger for find How the code works but i've no idea for which condition to check Time difference because some condtitions applied


Since you work on hours only, you have a set of condition in the day, and another one when you change of day in the shift.


这篇关于如何在VB中计算两次之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆