Results 1 to 5 of 5

Thread: [RESOLVED] Help checking date and hour

Hybrid View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] Help checking date and hour

    Hello! Is there any way to check date and hour without using two Timers?

    Right now i have this for the date:

    Code:
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer1.Stop()
            If Date.Today = My.Settings.SelectedDate Then
                'Do Something
            End If
        End Sub
    And for the time I don't know how to do it.... How can I do this using a Background Worker?

    Thanks in advance!

    PS: the Timer1.Interval = 3600000 miliseconds... That's one whole hour.

    PS (again): I thought about setting the Timer1 interval to 60000miliseconds, so that it would check the time everysingle step of the way (or maybe 10 minutes...), but I don't want the app to lag, so... How can i implement a Background Worker?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: Help checking date and hour

    Is there some reason why you can't use the NOW object?

    Plus, I have no idea why you would need two timers to check a date and time.

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Help checking date and hour

    Why not use the Windows Task Scheduler to schedule your application to launch at whatever interval you need to do.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Help checking date and hour

    I would probably set the (singular) timer to 1 second interval. In the tick event, check the 'now' time as Campion mentioned.

    Very rarely is there a necessity for two forms-based timers.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Exclamation Re: Help checking date and hour

    Quote Originally Posted by Pradeep1210 View Post
    Why not use the Windows Task Scheduler to schedule your application to launch at whatever interval you need to do.
    How do I use the windows task scheduler?

    EDIT:

    Ok, so i found this code that jmcilhinney posted 4 years ago...

    Code:
          Private Form1_Load(...) Handles MyBase.Load
              Me.SetAlarm()
          End Sub
          Private Sub Timer1_Tick(...) Handles Timer1.Tick
              'Set the alrm to Tick again at 2:00 PM tomorrow.
              Me.SetAlarm()
              'Do something here.
          End Sub
             Private Sub SetAlarm()
              Dim currentTime As Date = Date.Now
              Dim alarmTime As Date = Date.Today.AddHours(14)
              'If it is already past 2:00 PM set the alarm for tomorrow.
              If currentTime > alarmTime Then
                  alarmTime = alarmTime.AddDays(1)
              End If
              Dim timeToAlarm As TimeSpan = alarmTime.Subtract(currentTime)
              'Set the Timer's Interval.
              Me.Timer1.Interval = CInt(Math.Floor(timeToAlarm.TotalMilliseconds))
          End Sub
    So I change a line from Private Sub SetAlarm() to this:

    Code:
            Dim alarmDay As Date = DateTimePicker1.Value
            If currentTime > alarmTime Then
                alarmTime = alarmTime.AddDays(alarmDay.ToOADate())
            End If
    And I just wanted to know that if I select any day, let's say next week's wednesday, the alarm time value would change to "alarmtime + time to selected date". Or how could I calculate the days left until the alarm sets off?

    Thanks!

    EDIT 2:

    Nevermind, I think I answered myself. I'll post code later.
    Last edited by tassa; May 7th, 2009 at 07:19 PM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width