Time Check

Hoongz

Active member
Joined
Nov 22, 2007
Messages
32
Programming Experience
1-3
Is there any coding that let you check against the current time so you can perform a certain action like at 2pm or such?
 
this is to gt the current time

VB.NET:
MessageBox.Show(Now.TimeOfDay.ToString)

thefore you need to do some condition that will perform, as you said, at 2pm
 
basically i need to check for a certain timing. Can anyone teach me how it can be done? Do i need to code it such that it check once every minute for the time i need?
 
i'm not sure if i got your idea but lets say you need to perform a task on a given minute.

You needs a timer control. You need to set its time interval (the time in millisecords it will check your condition)

below is a stupid :) example of the timer control

VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Now.TimeOfDay.Minutes = 47 Then
            MessageBox.Show("47")
        End If
    End Sub

basically the above code will check if the mintute is 47 - if it is it will display a messagebox every 100miliseconds (that's why it is stupid)

can you transform this into your idea or am i out of track?

HTH
 
Oh it somehow helps. But are we able to check the time in 24 hour? Basically what i wanted to do is at at a certain time each day, the site will perform some action. so if the code is now.timeofday.hours = 02 it's 2am only or both 2am and 2pm?
 
hmmmm

i'm not 100% sure but i check this MSDN link: http://msdn2.microsoft.com/en-us/library/system.datetime.hour.aspx

in the:

VB.NET:
Dim moment As New System.DateTime(1999, 1, 13, 3, 57, 32, 11)

' Year gets 1999.
Dim year As Integer = moment.Year

' Month gets 1 (January).
Dim month As Integer = moment.Month

' Day gets 13.
Dim day As Integer = moment.Day

' Hour gets 3.
Dim hour As Integer = moment.Hour

' Minute gets 57.
Dim minute As Integer = moment.Minute

' Second gets 32.
Dim second As Integer = moment.Second

' Millisecond gets 11.
Dim millisecond As Integer = moment.Millisecond

the Hour is said to be 24hrs - therefore i think it can solve your problem
 
where would you be running the script from and what is going to trigger the time change? btw, shouldn't this be in the asp.net part of the forum if it's a site based problem as opposed to windows forms?
 
Basically i'm doing a advert display. So all i have is a wmp control in a form. What my lecturer wants me to do is to use vb.net and sql, storing the media as binary inside sql and retrieve them. Now he kills me by saying different adverts have different group play time, so basically once it reaches a certain time i'm suppose to drop the current playlist and get the new media that falls in the second play period and start playing those. I guess i need plenty of help from you guys
 
VB.NET:
Dim twoOclockToday As Date = Date.Today.AddHours(14)
Dim interval As Integer = CInt(Math.Round((twoOclockToday - Date.Now).TotalMilliseconds))

myTimer.Interval = interval
myTimer.Start()
The Timer will now Tick at 2 o'clock today, or fairly close to it.
 
VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Now.TimeOfDay.Minutes = 47 Then
            MessageBox.Show("47")
        End If
    End Sub
I think this code will be more suitable for my case. But does it work only on the moment it hits the minute or hour i set? I'm afraid it might miss the time and will not go into the function. Can a range be used?
 
Just a little question. For the above code. Does it do what ever function I asked it to perform each time it hits 47minute or it will only function once?
 
Is there any form of time check in vb.net? For example i want to do a function at 02:03:01 everyday. Will there be a function to take care of that? If there isn't anyone with alternative solutions for me? Thanks a million..
 
VB.NET:
Private alarmTime As New TimeSpan(2, 3, 1)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim alarmTime As Date = Date.Today + Me.alarmTime

    If alarmTime > Date.Now Then
        alarmTime.AddDays(1)
    End If

    Dim interval As TimeSpan = alarmTime - Date.Now

    'Set the Timer's Interval to the time until the next alarm.
    Me.Timer1.Interval = CInt(Math.Round(interval.TotalMilliseconds))
    Me.Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim alarmTime As Date = Date.Today.AddDays(1) + Me.alarmTime
    Dim interval As TimeSpan = alarmTime - Date.Now

    'Set the Timer's Interval to the time until the next alarm.
    Me.Timer1.Interval = CInt(Math.Round(interval.TotalMilliseconds))

    'Do something here.
End Sub
 
Back
Top