24hr time calculations

Anti-Rich

Well-known member
Joined
Jul 1, 2006
Messages
325
Location
Perth, Australia
Programming Experience
1-3
i am currently working on a form that handles my company's timesheets, which work in 24 hour time.

the following code would return the wrong values,
VB.NET:
[SIZE=2]TotalNormalHours = (([/SIZE][SIZE=2][COLOR=#0000ff]CDbl[/COLOR][/SIZE][SIZE=2](txtTimeOut.Text) / 100) - ([/SIZE][SIZE=2][COLOR=#0000ff]CDbl[/COLOR][/SIZE][SIZE=2](txtTimeIn.Text) / 100))[/SIZE]
[SIZE=2]
[/SIZE]

if i had somethink like 1500/100 - 1100/100, it would return 4, which is correct. however, if i did 1115/100 - 1100/100 it would return 0.15, now i realise that this represents a quarter of an hour, so how would i make it detect something like this and change the decimal on the end from 0.15 to 0.25 (and also 0.45 to 0.75).

if there are some 24hr time functions in vb.net could someone please tell me about them, as this would make life alot easier! :)

cheers all
regards
adam

 
TotalNormalHours = ((((CDbl(txtTimeOut.Text) / 100) - (CDbl(txtTimeIn.Text) /100)))/60)*100
this should do it
 
Last edited:
thanks epyk, i smacked myself (hard) in the head when i saw that.. lol ive done something like that before in another project, i just didnt think about it properly. cheers mate :)

another problem, how do i round up decimal places? ie. 0.7499999 to 0.75?

does anyone else know how to do this?
cheers guys, all your feedback is much appreciated
regards
adam
 
hmm, well thats not good :( does anyone know a different way of doing it? i cant afford for errors like that... you would think that they would have done it properly when creating the function!? thats really strange... it could be just a bug or something. im gonna create a function that does it manually, and ill post it when im done

cheers for all your input guys its much appreciated!! :)


regards
adam
 
ok, this is gonna sound dumb, but how is the decimal type different to the double type, and how do i use it,

i put this in my code...

VB.NET:
Dim c as Decimal = 1.22 'just a random number, nothing important

and it spat out the error 'option strict on disallows implicit conversions from double to decimal'. i understand what the error means, just not why it is happening. arent double and decimal the the same thing? in regards to they both handle numbers that might not be whole numbers?

if anyone could clarify i would be really grateful

cheers
adam
 
if there are some 24hr time functions in vb.net could someone please tell me about them
Time objects in Framework is represented by the DateTime and TimeSpan structures. DateTime is the Date data type. They both feature time arithmetic functions.
 
hey guys

finished the manual time calculation function! might not be the best way of doing it (and there probably are better ways of doing it), but as yet i dont know them (cue JohnH's reply next :p), so this will do me until i am made aware of these functions...

hope this helps someone else as well! :)

VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] TimeCalc([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] start [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] finish [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] s2 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] f2 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] iterations [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] startminutes [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = 0.0[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] finishminutes [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = 0.0[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] totalminutes [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Double[/COLOR][/SIZE][SIZE=2] = 0.0[/SIZE]
 
[SIZE=2]startminutes = (start / 100) - [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2]((start / 100))[/SIZE]
[SIZE=2]startminutes = Math.Round((startminutes / 60) * 100, 2)[/SIZE]
[SIZE=2]finishminutes = (finish / 100) - [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2]((finish / 100))[/SIZE]
[SIZE=2]finishminutes = Math.Round((finishminutes / 60) * 100, 2)[/SIZE]
[SIZE=2]totalminutes = finishminutes - startminutes[/SIZE]
 
[SIZE=2]s2 = (start / 100)[/SIZE]
[SIZE=2]f2 = (finish / 100)[/SIZE]
 
[SIZE=2]iterations = [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](f2) - [/SIZE][SIZE=2][COLOR=#0000ff]CInt[/COLOR][/SIZE][SIZE=2](s2)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]CDbl[/COLOR][/SIZE][SIZE=2](iterations) + totalminutes[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]

oh and if anyone could suggest improvements/modifications to the function, that would be great

regards
adam
 
Last edited:
but how do you convert 0700 (ie. 7am) into the timespan?
"7am" isn't a timespan, it is a datetime. Review the constructors available. Create a New Date(...)
 
ok, i had a look at the constructors, and the one that fits my situation (sort of), has about 5 different things to be supplied; year,month,day,hour,minute. this seems to be a really roundabout way of doing this. and then when i want to add the amount of hours i need to, i need to pluck the difference of hours between start and finish times to add it to the datetime. it seems that my function is an easier way of doing it.

i have a feeling i am missing something or doing something wrong. if you could help me out, that would be great,

cheers for your help so far, too
regards
adam
 
Date accepts various input formats, for example you can use its Parse method.
VB.NET:
dim d as date = date.parse("07:00")
d = d.addminutes(15)
 
dim datenow as date = date.now
dim span as timespan = datenow.subtract(d)
 
msgbox("difference in minutes is " & span.totalminutes)
You can also provide dummy values for the constructor for example if the date part of datetime doesn't matter.
 
Hey johnh

thanks for that! i adapted it to what i needed it to do and it works perfectly! i didnt know that you could use the parse method like that.

cheers for your help mate, much appreciated
regards
adam
 
Back
Top