如何从日期时间选择器值中获取小时和分钟的差异
问题描述:
大家好,
我已经开发了一个桌面应用程序,其中我已经从访问数据库中获取数据,即与日期时间选择器值之间的小时和分钟的差异,为此,我在下面编写了代码,但我没有得到分钟数的差异.
Hi Everyone,
I have developed a desktop application in which i have fetch data from access database i.e difference of hours and minutes from datetime picker values, to get this i have written the code below but I am not getting the minutes difference.
mStrFinalQuery = "select VehicleMaster.RegistrationNo,VehicleTransaction.Barcode,VehicleMaster.VehicleType,VehicleMaster.VehicleModel,format(VehicleTransaction.TranInDate,""dd-MM-yyyy HH:mm:ss"") as InDate,format(VehicleTransaction.TranOutDate,""dd-MM-yyyy HH:mm:ss"") as OutDate,format(TranInDate,""HH"")-format(TranOutDate,""HH"") AS TotalOutHour,format(TranInDate,""mm"")-format(TranOutDate,""mm"") AS TotalMins from VehicleMaster inner join VehicleTransaction on VehicleMaster.Barcode = VehicleTransaction.Barcode where TranDate>= format('" & DateTimePicker1.Text & "',""dd-MM-yyyy"") and TranDate<= format('" & DateTimePicker2.Text & "',""dd-MM-yyyy"") order by TransactionId"
谢谢您的帮助.
Any help is kindly appreciated
答
由于您通过传递日期时将其格式化的方式浪费了DateTimePicker的时间部分,因此无济于事,但是随后您将一个很好的日期时间值转换为字符串,以便从中减去另一个字符串!
使用 DateDiff函数 [
Since you are throwing away the time portion of the DateTimePicker by formatting it when you pass it through, that isn''t going to help, but then you convert a perfectly good date time value to a string in order to subtract another string from it!
Use the DateDiff function[^] instead...
And pass your values as DateTime via a parametrised query rather than converting them to a string in the first place!