473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to subtract hour from DateTime in VB

103 100+
I have a line of code which consists of two variables. One is a string called "endTime" that is formated like 05:00 PM the other is a string called "overTime" that is formated like "1". What I am trying to do is subtract the overTime from the endTime. I have the following line that converts the strings to Dates, however the subtraction returns 12:00 AM, but I am expecting 04:00 PM. Here is the code line:
Expand|Select|Wrap|Line Numbers
  1. timeDiff = CDate(endTime) - Hour(CDate(overTime))
Can anyone tell me what I am doing wrong and how I should go about it?
Nov 5 '12 #1
4 5236
zmbd
5,501 Expert Mod 4TB
Why not use the datediff() function?
http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
Nov 5 '12 #2
Brian Connelly
103 100+
@zmbd
DateDiff would return the diff. in time between and one is a date the other was not. The actual solution to what I was tring to do used dateAdd(). The code is this:
Expand|Select|Wrap|Line Numbers
  1. timeDiff = DateAdd("h", overTime * -1, endTime)
The "h" is the metric of an hour, the overTime * -1 makes the value a negative (so Im adding a negative number to the time, and the endTime is the Time variable that I am changing.

I do appreciate your response and help.
Nov 5 '12 #3
zmbd
5,501 Expert Mod 4TB
... and yet your op and the code you posted were for date data types.

So... you are after what exactly? Something along the lines of if Sam worked three hours of overtime and Sam punched off the clock at 17h00 at what time did Sam's regular time end? (answer is 14h00) If that was what you were after, and had worded your question along that lines, then my answer would have been to use the dateadd() as you have done.
Nov 5 '12 #4
Killer42
8,435 Expert 8TB
Probably the DateAdd function would be more appropriate.

The way it's used may vary depending on your version of VB. I've linked to the doco for VB6.
Nov 6 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Symon R | last post by:
This is a bit of a weird one that I haven't yet been able to solve - I'm hoping someone out there can disprove my findings and tell me where I've gone wrong! I have designed a web service that...
0
by: PAUL MENARD | last post by:
--0-930715088-1061086093=:22602 Content-Type: text/plain; charset=us-ascii Hello All, I'm having trouble understanding the MySQL docs on how to subtract two DATETIME values. I have two tables...
6
by: Thomas Bartkus | last post by:
MySQL Version 4.0.20 on a Linux server. How does one get the elapsed time between (2) DateTime values? I need the answer to the nearest minute. Is upgrading to Ver 5 with its more robust...
3
by: bredal Jensen | last post by:
Hello gurus, I'm building a small booking system and i have come accross quiet a tedious pitfall. "I need to make sure that people do not book for tomorrow when todays time is greater or...
5
by: Salad | last post by:
I wrote a routine for somebody yesterday. When I compare datStart to datEnd in the DoWhile comamnd, when the date/times match they don't. For example, 1:00:00 PM does not match 1:00:00 PM. I...
5
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new...
1
by: JIM.H. | last post by:
Hello I have TextBox1 this keeps date and time information and I need to compare this DateTime with the current DateTime and find difference in hour. How can I do this in VB.Net? Thanks,
2
by: Simon Gare | last post by:
Hi all, need to subtract one date from another to show how long a user was logger in for. i.e. <%=(rsDriverLogin.Fields.Item("OFF_DATE").Value)%> minus...
2
by: deepbright | last post by:
I'm having trouble understanding the MySQL docs on how to subtract two DATETIME values. I have two tables that have a DATETIME column. In my SELECT I am doing a JOIN to bring in both sets of rows....
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.