Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hour and minutes inrterval step 30 minutes 3

Status
Not open for further replies.

sal21

Programmer
Apr 26, 2004
425
IT
Dim DA As Date, A As Date

for example:

DA=07:00
A=09:30

How to create a list oh time with a step of 30 minutes

to the and i need

07:00
07:30
08:00
08:30
09:00
09:30
 
What have you tried so far?
Show some code of your attempts.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Code:
 Dim D As Date
    For D = DA To A Step 30
        Debug.Print D
    Next D

but dont work[2thumbsup]
 
One way:
Code:
Call ShowTimes("7:00", "9:00", 30)

Code:
Private Sub ShowTimes(ByRef StartTime As String, ByRef StopTime As String, ByRef Interval As Integer)
Dim dtStop As Date
Dim dt As Date

dt = CDate(StartTime)
dtStop = CDate(StopTime)

Debug.Print dt

Do While dt <= dtStop
    dt = DateAdd("n", Interval, dt)
    If dt > dtStop Then Exit Do
    Debug.Print dt
Loop

End Sub

output:
[pre]
7:00:00 AM
7:30:00 AM
8:00:00 AM
8:30:00 AM
9:00:00 AM [/pre]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
ops....
but i required to redim DA and A in As String, or not?
 
I don't know what you mean by "redim DA and A in As String"

Date (and Time) is just a Number, so you can add to it or subtract from it.
You cannot add or subtract any value(s) from Strings. You can (kind of) "add" - concatenate - to a String, but not 'add' as math adding.

If you just add a number to a Date, you are adding Days (not minutes, or seconds, or whatever):

Code:
Dim DA As Date, A As Date
Dim D As Long

DA = Date
A = Date + 120

For D = DA To A Step [red]30[/red]
    Debug.Print CDate(D)
Next D

Today, from above code you get as an output in [red]30[/red] days increments:[pre]
3/28/2022
4/27/2022
5/27/2022
6/26/2022
7/26/2022
[/pre]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
If working with date type:
Code:
Dim DA As Date, A As Date, D As Date
DA = TimeSerial(7, 0, 0)
A = TimeSerial(9, 30, 0)
For D = DA To A Step (1 / 48)
    Debug.Print D
Next D


combo
 
>but i required to redim DA and A in As String, or not?

No, not necessarily - you could just modify the procedure definition

[tt]Private Sub ShowTimes(ByRef StartTime As Date, ByRef StopTime As Date, ByRef Interval As Integer)[/tt]


But as you can see - there are many ways to skin this particular cat ...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top