Date Functions

Hello there

I'm struggling with day, time functions and compare ..
I'd like to run a piece a code if;

1. it's not a Saturday or Sunday and between Mytimes
2. and if it's between Mytimes compare how many minutes left before Myendtime then
3. execute code

[code]
Dim myTime As Date
myTime = Format(Now(), "hh:mm:ss tt")


If vbSaturday Or vbSunday AndAlso myTime >= TimeSerial(7, 0, 0) _
And vbSaturday Or vbSunday AndAlso myTime < TimeSerial(9, 55, 0) Then
MsgBox ("1st piece")

ElseIf myTime >= TimeSerial(10, 0, 0) And myTime < TimeSerial(15, 0, 0) Then
MsgBox ("2nd piece")

Else
MsgBox ("3rd piece")
End If
[/code]

thanks Chris

Comments

  • : Hello there
    :
    : I'm struggling with day, time functions and compare ..
    : I'd like to run a piece a code if;
    :
    : 1. it's not a Saturday or Sunday and between Mytimes
    : 2. and if it's between Mytimes compare how many minutes left before
    : Myendtime then
    : 3. execute code
    :
    : [code]:
    : Dim myTime As Date
    : myTime = Format(Now(), "hh:mm:ss tt")
    :
    :
    : If vbSaturday Or vbSunday AndAlso myTime >= TimeSerial(7, 0, 0) _
    : And vbSaturday Or vbSunday AndAlso myTime < TimeSerial(9, 55, 0) Then
    : MsgBox ("1st piece")
    : 'execute code here? give a comment on where the code is executed

    : ElseIf myTime >= TimeSerial(10, 0, 0) And myTime < TimeSerial(15, 0, 0) Then
    : MsgBox ("2nd piece")
    :
    : Else
    : MsgBox ("3rd piece")
    : End If
    : [/code]:
    :
    : thanks Chris


    i can see your problem or most of it anyways because i am doing timed events with my made from scratch program and i put in custom hours and minutes in my program but seconds doesnt work as well with me i tryed it.

    code i changed:

    If vbSaturday Or vbSunday AndAlso myTime >= TimeSerial(7, 0, 0) _
    : And vbSaturday Or vbSunday AndAlso myTime <= TimeSerial(9, 55, 0) Then

    'changed myTime < TimeSerial to myTime <= TimeSerial

    : MsgBox ("1st piece")
    : 'execute code here? give a comment on where the code is executed

    : ElseIf myTime >= TimeSerial(10, 0, 0) And myTime <= TimeSerial(15, 0, 0) Then

    'minutes between endtimes if thats what this is i dont think the above line will work set a timer and set the number of milli-seconds to the amount of seconds converted will equal the minutes you want. example 1000 milli-seconds equals 1 second i thinking out of memory or store the hours , minutes , and seconds in different variables to have custom selected time compares.
    'Sample code for custom time variables: Dim StartHour,StartMinute,StartSecond as Integer
    Dim EndHour,EndMinute2,EndSecond2 As integer
    Dim StartTimeString as string
    Dim EndTimeString as string
    'should work but if this doesnt start with just the hour and minutes and that is way more likely to work

    StartTimeString = CStr(StartHour) + ":"+ CStr(StartMinute) +":"+ CStr(StartSecond)

    EndTimeString = CStr(EndHour) +":"+ CStr(EndMinute)+":"+CStr(EndSecond)

    'my timed events are ones done at a selected time and date

    : MsgBox ("2nd piece")
    :
    : Else
    : MsgBox ("3rd piece")
    : End If

    hope this helps but if theres more post so you can get help

  • Thanks
    I tried a couple of ways and made some changes, however if I do this now in debugging mode it works, it stops the timer and display msgbox.
    But if I run this outside it does not stop timer and does not display msgbox..

    [code]
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Timer1.Tick

    Timer1.Start()

    Label1.Text = formatDigits(Now.Hour) & ":" _
    & formatDigits(Now.Minute) & ":" & formatDigits(Now.Second)

    Shutdown()

    End Sub

    Private Sub Shutdown()

    Dim myTime As Date = Format(Now(), "hh:mm:ss tt")
    Dim StartHour, StartMinute As Integer
    Dim EndHour, EndMinute As Integer
    Dim StartTime As String
    Dim EndTime As String

    StartHour = 6
    StartMinute = 0
    EndHour = 11
    EndMinute = 16
    StartTime = TimeValue(StartHour & ":" & StartMinute)
    EndTime = TimeValue(EndHour & ":" & EndMinute)

    If myTime >= StartTime And myTime < EndTime Then

    If String.Compare(Now.Hour.ToString, EndHour.ToString) = 0 Then
    If String.Compare(Now.Minute.ToString, EndMinute.ToString) = 0 Then
    Timer1.Stop()
    MsgBox("1st piece")
    End If
    End If

    End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click

    Timer1.Enabled = True
    Timer1.Start()

    End Sub

    Private Function formatDigits(ByVal Value As Integer) As String

    Dim newValue As String
    Dim len As Integer = Value.ToString.Length

    If len = 1 Then
    newValue = "0" + Value.ToString
    Return newValue
    Else
    Return Value.ToString
    End If

    End Function
    [/code]

    I looked at several possibilities and no luck any ideas why this might be?
  • my fault i forgot to add the am pm parts of the time indicators look at the code below for changes made that should make the time strings correct and stop some errors from occuring that may be one of the errors.
    hope this helps i want to try to help ask me if you need any more help


    : Thanks
    : I tried a couple of ways and made some changes, however if I do this
    : now in debugging mode it works, it stops the timer and display
    : msgbox.
    : But if I run this outside it does not stop timer and does not
    : display msgbox..
    :
    : [code]:
    : Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    : Handles Timer1.Tick
    :
    : Timer1.Start()
    :
    : Label1.Text = formatDigits(Now.Hour) & ":" _
    : & formatDigits(Now.Minute) & ":" & formatDigits(Now.Second) 'add & Space(1) & "PM" or & Space(1) & "AM" you have to add the pm am indicator yourself in code or figure out how in code to get it
    :
    : Shutdown()
    :
    : End Sub
    :
    : Private Sub Shutdown()
    :
    : Dim myTime As Date = Format(Now(), "hh:mm:ss tt")
    : Dim StartHour, StartMinute As Integer
    : Dim EndHour, EndMinute As Integer
    : Dim StartTime As String
    : Dim EndTime As String
    :
    : StartHour = 6
    : StartMinute = 0
    : EndHour = 11
    : EndMinute = 16
    : StartTime = TimeValue(StartHour & ":" & StartMinute & Space(1)& am or pm string indicator as in the timer1 tick event above)
    : EndTime = TimeValue(EndHour & ":" & EndMinute & am pm indicator do same as above)
    :
    : If myTime >= StartTime And myTime < EndTime Then

    : 'in the line below check to to short time string methods on endhour to see if you can get just the hour with it or try this line of code: EndHour.Hour.tostring because in the code below you are doing the whole time string on EndHour and comparing the whole time string to just the string with the hour which i could see wouldnt work. Example of output: String.Compare("2","2:03:03") now with this example can you see how this wouldnt work make end hours string be just an hour string or vis versa change the now line to: now.to short time string

    : If String.Compare(Now.Hour.ToString, EndHour.ToString) = 0 Then
    : If String.Compare(Now.Minute.ToString, EndMinute.ToString) = 0 Then
    : Timer1.Stop()
    : MsgBox("1st piece")
    : End If
    : End If
    :
    : End If
    :
    : End Sub
    :
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    : Handles Button1.Click
    :
    : Timer1.Enabled = True
    : Timer1.Start()
    :
    : End Sub
    :
    : Private Function formatDigits(ByVal Value As Integer) As String
    :
    : Dim newValue As String
    : Dim len As Integer = Value.ToString.Length
    :
    : If len = 1 Then
    : newValue = "0" + Value.ToString
    : Return newValue
    : Else
    : Return Value.ToString
    : End If
    :
    : End Function
    : [/code]:
    :
    : I looked at several possibilities and no luck any ideas why this
    : might be?
    :
  • Thanks again - with some help I changed it to the following and the event now fires as it should.

    [code]Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    Label1.Text = DateTime.Now().ToString("HH:mm:ss")
    Shutdown()

    End Sub
    Private Sub Shutdown()

    Dim StartTime As New DateTime(Now.Year, Now.Month, Now.Day, 8, 53, 0)
    Dim EndTime As New DateTime(Now.Year, Now.Month, Now.Day, 8, 55, 0)

    If Now() <= StartTime Or Now() >= EndTime Then
    Timer1.Stop()
    MsgBox("1st piece")
    End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Timer1.Start()

    End Sub[/code]

    If I want this code to run in the background while other applications are running, will this affect other resources? Should I rather make use of threading? How would I go about?
    Chris

  • : Thanks again - with some help I changed it to the following and the
    : event now fires as it should.
    :
    : [code]: Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    :
    : Label1.Text = DateTime.Now().ToString("HH:mm:ss")
    : Shutdown()
    :
    : End Sub
    : Private Sub Shutdown()
    :
    : Dim StartTime As New DateTime(Now.Year, Now.Month, Now.Day, 8, 53, 0)
    : Dim EndTime As New DateTime(Now.Year, Now.Month, Now.Day, 8, 55, 0)
    :
    : If Now() <= StartTime Or Now() >= EndTime Then
    : Timer1.Stop()
    : MsgBox("1st piece")
    : End If
    :
    : End Sub
    :
    : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    :
    : Timer1.Start()
    :
    : End Sub[/code]:
    :
    : If I want this code to run in the background while other
    : applications are running, will this affect other resources? Should I
    : rather make use of threading? How would I go about?
    : Chris
    :
    :

    i only know mostly about asp threading and some about differences between mdi and single form applications. this application i know can be minimized to reduced some system resoures but thats not the biggest factor of affecting system resources. graphics intense programs , video editing , animation editing, and video games take up a lot of resouces so you dont have to worry about you program taking up resouces unless you have an error in your loops or a sub or code that makes it repeat indifinitly or till it causes your app to produce an error. if you have indifinite loops make sure to fix them so that some code somewhere will cause it to escape or change code so that you can exit the sub or loop. i would go about checking for errors like the ones above unless there is graphic programming commands you have involved (etc timed events in programming graphic simulations and programming with direct x). i think this code looks fine and doesnt need to be changed but you can look up threading and ask more people about it but the code above i think is usable and shouldnt take up a lot of system resoures.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories