473,397 Members | 2,077 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,397 software developers and data experts.

Looping the time

lotus18
866 512MB
Dear programmers;

I want to shorten my codes in adding a time on a combo box with 30 mins time interval

My sample code

Expand|Select|Wrap|Line Numbers
  1. Combo1.Additem "7:30 AM"
  2. Combo1.Additem "8:00 AM"
  3. Combo1.Additem "8:30 AM"
  4. Combo1.Additem "9:00 AM"
  5. Combo1.Additem "9:30 AM"
  6. Combo1.Additem "9:00 AM"
  7. Combo1.Additem "9:30 AM"
  8. Combo1.Additem "10:00 AM"
  9. Combo1.Additem "10:30 AM"
  10. Combo1.Additem "11:00 AM"
  11. Combo1.Additem "11:30 AM"
  12. Combo1.Additem "12:00 PM"
  13. Combo1.Additem "12:30 PM"
  14. Combo1.Additem "1:00 PM"
Nov 7 '07 #1
16 1508
Killer42
8,435 Expert 8TB
I'd suggest you create a variable of type Date. Set it to the start time (in this case, 07:30 I suppose). Then do a loop using the DateAdd() function to add 30 minutes, until it reaches the finish time. Each time around the loop, use the Format() function to get the string you want, and add it to the list.

Note, this applies to VB6 and may need modification if using a later version.
Nov 8 '07 #2
lotus18
866 512MB
I did not try it but I just want to say thank you for your reply. If you won't mind, can you please send me the codes :)
Nov 11 '07 #3
debasisdas
8,127 Expert 4TB
I did not try it but I just want to say thank you for your reply. If you won't mind, can you please send me the codes :)
Further don't ask for code before trying yourself what was suggested to you.
Nov 11 '07 #4
lotus18
866 512MB
Sorry guys... I tried and I've spend so much time but still i can't make it...
Can you please tell me what's wrong with these codes?


Expand|Select|Wrap|Line Numbers
  1. Dim d As Date
  2. d="#7:00 AM#"
  3. Do Until d = #8:00 PM#
  4. Combo1.AddItem DateAdd("n", 30, d)
  5. Loop 
  6.  
Nov 11 '07 #5
Killer42
8,435 Expert 8TB
Sorry guys... I tried and I've spend so much time but still i can't make it...
Can you please tell me what's wrong with these codes?
Sure. You've got unnecessary quotes on line 2, and you're not incrementing the value of d as you go around the loop (which means it will go forever).

Also, I suspect you will want to use the Format() function to format the value that you place in the combobox. But that can wait until you get the loop working.
Nov 11 '07 #6
lotus18
866 512MB
hello killer42. This is my modification.

Expand|Select|Wrap|Line Numbers
  1. Dim i As Long
  2. Dim d As Date
  3. Dim e As Date
  4.  
  5. d=#7:00:00 AM#
  6.  
  7. Do
  8. e=DateAdd("n", i, d)
  9. Combo1.AddItem e
  10. i=i+30 'add 30 mins interval
  11. Loop Until i=810 '8:00:00 PM
These codes work but I can't really make it how to add 30 mins interval on variable "e". It goes looping and looping as you've said. By the way, regarding about the Format function I can't add AM/PM at the end and also the time format is 24-hour. How to change it to 12-hour format?
Nov 12 '07 #7
Killer42
8,435 Expert 8TB
hello killer42. Is this one correct?
It's looking pretty good. But why not just reuse d instead of creating another variable e? And I wouldn't increment i; you're going to be incrementing your time by half an hour longer each time. Just leave i as it is, and loop until d hits the finishing time.

As for Format(), hit the books. :) The documentation describes it pretty thoroughly.
Nov 12 '07 #8
lotus18
866 512MB
Thanks for your immediate reply, killer42 :)
Nov 12 '07 #9
jamesd0142
469 256MB
if it help i came up with this...

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim start1 As String = "00:00"
  3.     Dim end1 As String = "12:00"
  4.     Dim i As Integer
  5.     Dim a As String
  6.     Dim start2 As String
  7.  
  8.  
  9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  10.  
  11.         box.Items.Clear()
  12.  
  13.         box.Items.Add(start1)
  14.         'For i = 1 To Len(start1)
  15.         '    a = Mid(start1, i, 1)
  16.         '    If a >= "0" Or a <= "9" Then
  17.         '        start2 = start2 + a
  18.         '    End If
  19.         'Next
  20.         'start1 = start2
  21.  
  22.         'take out :
  23.  
  24.         While start1 < end1
  25.             start2 = ""
  26.             For i = 1 To Len(start1)
  27.                 a = Mid(start1, i, 1)
  28.                 If a = "0" Or a = "1" Or a = "2" Or a = "3" Or a = "4" Or a = "5" Or a = "6" Or a = "7" Or a = "8" Or a = "9" Then
  29.                     start2 = start2 + a
  30.                 End If
  31.             Next
  32.             start1 = start2
  33.  
  34.             'Dim convert As String
  35.             a = Mid(start1, 3, 2)
  36.             If a = "30" Then
  37.                 start1 = start1 - 30
  38.                 start1 = start1 + 100
  39.             Else
  40.                 start1 = start1 + 30
  41.             End If
  42.  
  43.             'convert here
  44.             If Len(start1) = 3 Then
  45.                 start1 = "0" + Mid(start1, 1)
  46.             End If
  47.             If Len(start1) = 2 Then
  48.                 start1 = "00" + Mid(start1, 1)
  49.             End If
  50.  
  51.             start1 = Mid(start1, 1, 2) & ":" & Mid(start1, 3, 2)
  52.             box.Items.Add(start1)
  53.  
  54.         End While
  55.  
  56.     End Sub
  57. End Class
  58.  
Nov 12 '07 #10
Killer42
8,435 Expert 8TB
if it help i came up with this...
Thanks for that, James. Does it work? I have to admit, it seems like a lot of extra coding for quite a simple task.
Nov 13 '07 #11
lotus18
866 512MB
Hello James

I'm looking for a vb 6.0 code, but thanks for your reply. :)
Nov 13 '07 #12
jamesd0142
469 256MB
Thanks for that, James. Does it work? I have to admit, it seems like a lot of extra coding for quite a simple task.
haha killer you should know by now... i don't write 3 lines of code when i can write 53 lines ;)
Nov 13 '07 #13
lotus18
866 512MB
haha killer you should know by now... i don't write 3 lines of code when i can write 53 lines ;)
I think in solving a problem like this we should apply the KISS principle.
Nov 13 '07 #14
QVeen72
1,445 Expert 1GB
hello killer42. This is my modification.

Expand|Select|Wrap|Line Numbers
  1. Dim i As Long
  2. Dim d As Date
  3. Dim e As Date
  4.  
  5. d=#7:00:00 AM#
  6.  
  7. Do
  8. e=DateAdd("n", i, d)
  9. Combo1.AddItem e
  10. i=i+30 'add 30 mins interval
  11. Loop Until i=810 '8:00:00 PM
These codes work but I can't really make it how to add 30 mins interval on variable "e". It goes looping and looping as you've said. By the way, regarding about the Format function I can't add AM/PM at the end and also the time format is 24-hour. How to change it to 12-hour format?
Hi,

Try this :
Expand|Select|Wrap|Line Numbers
  1.     Combo1.Clear
  2.     Dim t As Date
  3.     Dim i As Integer
  4.     t = CDate("07:00:00")
  5.     Do
  6.         t = DateAdd("n", 30, t)
  7.         Combo1.AddItem Format(t, "hh:mm AM/PM")
  8.         i = i + 1
  9.     Loop Until i >= 26
  10.  
  11.  
Regards
Veena
Nov 13 '07 #15
lotus18
866 512MB
Hi,

Try this :
Expand|Select|Wrap|Line Numbers
  1.     Combo1.Clear
  2.     Dim t As Date
  3.     Dim i As Integer
  4.     t = CDate("07:00:00")
  5.     Do
  6.         t = DateAdd("n", 30, t)
  7.         Combo1.AddItem Format(t, "hh:mm AM/PM")
  8.         i = i + 1
  9.     Loop Until i >= 26
  10.  
  11.  
Regards
Veena
Hi Veena

I just want to say THANK YOU VERY MUCH. Atlast! I've got what I wanted to know ;)
Nov 13 '07 #16
jamesd0142
469 256MB
ye this just demonstrates how much i have to learn :)
Nov 13 '07 #17

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

Similar topics

8
by: kaptain kernel | last post by:
i've got a while loop thats iterating through a text file and pumping the contents into a database. the file is quite large (over 150mb). the looping causes my CPU load to race up to 100 per...
7
by: Stegga | last post by:
Hi there, I'm trying to add 1 second incrementally to a timestamp while looping through it and I am pulling my hair out. Please can someone help me? example of code: $calc_timestamp =...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
3
by: Luke - eat.lemons | last post by:
Sorry for the post in this NG but im short on time to get this working and i haven't seem to of got a response anywhere else. Im pretty new to asp so all light on this question would be great. ...
22
Dököll
by: Dököll | last post by:
Hiya, Partners! I have been into it for 12 hours straight this week-end, my son is very unhappy. Looks like I am getting pretty close but need your help, Again. I will post my first...
0
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
4
by: XpatienceX | last post by:
Hi, Can someone help me with this assignment, I am confused of what is needed to be done here. I am suposed to design a program that models a worm's behavior in the following scenario: A...
3
by: assgar | last post by:
Hi I am having problem with my loping. I don't know if I have chosen the correct approach. GOAL: I need to insert into a table event types for a specific date range. The calendar the event...
2
by: Davaa | last post by:
Dear all, I am a student making a MS Form application in C++. I would ask a question about "Timer". Sample code which I am developing is below. private: System::Void...
3
by: tddeffner | last post by:
I have a form i need to create that involves looping dropdowns in asp.net. The form sets a time in a open time and a close time dropdownlist for each weekday. So I need it to loop seven times. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.