English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm creating a program to calculate total hours worked, while taking out lunches, for my boss...seen here...

http://answers.yahoo.com/question/index?qid=20070318175500AA6pqfq&r=w

I pretty much have an idea of what to do now, but what code would I enter to set a certain value to a string? Also, where would I put that code in the overall code? I want to make the data like this...

1AM = 1
2AM = 2
3AM = 3
4AM = 4
5AM = 5
6AM = 6
7AM = 7
8AM = 8
9AM = 9
10AM = 10
11AM = 11
12PM = 12
1PM = 13
2PM = 14
3PM = 15
4PM = 16
5PM = 17
6PM = 18
7PM = 19
8PM = 20
9PM = 21
10PM = 22
11PM = 23
12AM = 24

That way, if someone entered 7AM - 4PM, it would take 4PM's 16, and take away 7AM's 7 from it, to equal 9. After that I would take an hour out for a lunch hour and I know it's something like IF mondaytotal > 9 -1 or something like that. If someone could help me out on the part with defining numbers with the AMs and PMs, and/or help me out with lunch part, it would be greatly appreciated!

2007-03-18 15:27:36 · 2 answers · asked by alphaaxl 1 in Computers & Internet Programming & Design

2 answers

You can struggle and reinvent the wheel...

Why are you not using the Date Time Picker? (format property set to TIME) The value of which is a system DATE. The DATE data type represents date/times from 1JAN0001 at midnight to 31DEC9999. The underlying real number uses whole numbers to represent days and the decimal fraction to represent the time.

The DATE data type is also easily converted to a TIMESPAN which will let you deal with just the time portion of a DATE.

You can then subtract one TIMESPAN from another and easily get hours,minutes and seconds.

Here is an example of how to read a datetime picker value and convert it into a timespan which will let you extract hrs,minutes and seconds.

Dim dte As Date
Dim spn As TimeSpan
Dim hrs, min, sec As Int16

dte = Me.DateTimePicker1.Value
spn = dte.TimeOfDay
hrs = spn.Hours
min = spn.Minutes
sec = spn.Seconds

2007-03-18 16:19:18 · answer #1 · answered by MarkG 7 · 1 0

Dim TimeStr1, TimeStr2 As String
Dim Hour1, Hour2, TimeDiff As Integer
Dim AM As Boolean

If Len(TimeStr1) = 3 Then 'Make sure length is 4 characters
TimeStr1 = " " + TimeStr1
End If

If Right(TimeStr1, 2) = "AM" Then 'Check if AM is there
AM = True
Else:
AM = False
End If

Hour1 = CInt(Left(TimeStr1, 2))
If AM = False Then 'If not AM then convert to mil. time
Hour1 = Hour1 + 12
End If
'------------Do same thing w/ other string
If Len(TimeStr2) = 3 Then
TimeStr2 = " " + TimeStr2
End If

If Right(TimeStr2, 2) = "AM" Then
AM = True
Else:
AM = False
End If

Hour2 = CInt(Left(TimeStr2, 2))
If AM = False Then
Hour2 = Hour2 + 12
End If
'-------Subtract 'em
TimeDiff = Hour1 - Hour2

2007-03-18 15:59:29 · answer #2 · answered by ◄DiscoAsimov► 2 · 0 2

fedest.com, questions and answers