display date and time

I cants eem to get the datetime things to work.

I want to be able to display a label with

Day of week, day of month, month, year, hour, min, am/pm

Any advice appreciated. I cnat find any samples on the forums or microsoft or web.

I get this to work for the time at least

Public Class Form1

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

Label1.Text = Now.ToShortTimeString

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Timer1.Interval = 1000

Timer1.Start()

End Sub

End Class



Answer this question

display date and time

  • Bolugbe

    No these mistakes are all mine.

    I had tinkered with vb6, mostly doing vba for my dragon speaking stuff.

    I have alot of date or time stuff for dragon use. Particularly future dates figuring.

    I am finding visual studio 2005 different.

    The answer was staring me in the face.

    Private WithEvents timer1 As New System.Windows.Forms.Timer

    It works now fine.

    thx for your help, appreciate it.


  • MurrayS

    this works well on a blank form

    Public Class Form1

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

    Me.Label1.Text = DateTime.Now.ToString("dddd, d MMM yyyy h:mm tt")

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Timer1.Interval = 1000

    Timer1.Start()

    End Sub

    End Class

    but I cant get it to work on a form I am using to make a rtf editor.

    i get handle errors and timer declaratione errors. I have tried withevents and dim's in various ways. something is amiss

    the basic code

    Imports System.Drawing

    Imports System.Drawing.Image

    ' Rich Text Editor

    Public Class frmMain

    Declarations section

    Private currentFile As String

    Menu section

    Toolbar section

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Timer1.Interval = 1000

    Timer1.Start()

    End Sub

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

    Me.Label1.Text = DateTime.Now.ToString("dddd, d MMM yyyy h:mm tt")

    End Sub

    End Class


  • ManjuVijay

    I had tried the system and forms, didnt work so figured i didnt need themfor forms controls/label

    Imports System.Drawing

    Imports System.Drawing.Image

    Imports System

    Imports System.Windows.Forms

    ' Rich Text Editor

    Public Class frmMain

    Declarations section

    Private currentFile As String

    Menu section

    Toolbar section

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Timer1.Interval = 1000

    Timer1.Start()

    End Sub

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

    Me.Label1.Text = DateTime.Now.ToString("dddd, d MMM yyyy h:mm tt")

    End Sub

    End Class

    gets me errors:

    black large font above

    Name timer1 is not declared

    Name timer1 is not declared

    Handles clause requires a withevents variable defined in the containing type or one of its boxes

    this is where i had tried declarations and withevent statements but it still wouldnt work.


  • Davicho

    sort of like

    Namespace mytimelabel

    <------insert display time code here


    End Namespace ' mytimelabel



  • Speeding Target

    it says

    ToStringtimeString() is not a member of date.

    Using the code I posted shows a label with time perfectly.

    Adding the day of week, day of month, month and year is what i cant seem to pull off

    Saturday, 20 Jan 2006 1:00 am

    as an example


  • Robertis Tongbram

     CharlieRussell wrote:

    sort of like  

    Namespace mytimelabel

    <------insert display time code here

      
    End Namespace ' mytimelabel

     

    Not quite :-)

    Imports System.Drawing

    Imports System.Drawing.Image

    Imports System

    Imports System.Windows.Forms

     

     

    ' Rich Text Editor

     

    Public Class frmMain

     

     

    Declarations section

        Private currentFile As String

     

    Menu section

     

    Toolbar section

     

     

        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

            Timer1.Interval = 1000

            Timer1.Start() 

          

        End Sub 

     

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

     

            Me.Label1.Text = DateTime.Now.ToString("dddd,    d MMM yyyy    h:mm tt")

     

        End Sub 

    End Class

     

     

    If you still get errors, please post the exact errors as well as the line its complaining about :-)



  • dkoco

    Charlie,

    That code looks good. Have you set a break point on the timer event to see if the event is firing



  • vijayshankark

    this works well on a blank form

    Public Class Form1

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

    Me.Label1.Text = DateTime.Now.ToString("dddd, d MMM yyyy h:mm tt")

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Timer1.Interval = 1000

    Timer1.Start()

    End Sub

    End Class

    but I cant get it to work on a form I am using to make a rtf editor.

    i get handle errors and timer declaratione errors. I have tried withevents and dim's in various ways. something is amiss

    the basic code

    Imports System.Drawing

    Imports System.Drawing.Image

    ' Rich Text Editor

    Public Class frmMain

    Declarations section

    Private currentFile As String

    Menu section

    Toolbar section

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Timer1.Interval = 1000

    Timer1.Start()

    End Sub

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

    Me.Label1.Text = DateTime.Now.ToString("dddd, d MMM yyyy h:mm tt")

    End Sub

    End Class


  • Amos Soma

    my apologies - it should be ToShortTimeString()

  • Jassim Rahma

    you need to import the System.Windows.Forms namespace also, since the Timer and label are part of the Windows Forms

  • Toddap_MS

    tried doing:

    Me.Label1.Text = DateTime.Now.ToStringTimeString()

    what happens when you do this Does the problem still happen In general what happens when it doesnt work Nothing happens when trying to show the date/time Can you check to see if the timer event is being fired by perhaps placing as suggested a breakpoint in the timer tick event



  • enric vives

    ahhhh now I see what I was doing wrong.

    Thank you, realy appreciate the help.

    Thanks again for teaching.


  • Weavor

    at this point I'm stuck for now. All I can suggest is I hope you did not just copy and paste code :-) As there are a few designer codes placed in the designer.vb file that needs to be put into the new form also, in it's designer file

     

    if you like, send over the working project and non working project (seperate files) via my email addy (look in profile, click my name) and ill be sure to post the solution here



  • Andy Britcliffe

    ok....its the formatting you are after

    in regards to the ToStringTimeString() - I posted back giving my correction.

    so back to the formatting, try this:

    Me.Label1.Text = DateTime.Now.ToString("dddd, d MMM yyyy h:mm tt")

    look at the Custom DateTime Format string for further customization:

    http://msdn2.microsoft.com/en-us/library/8kb3ddd4.aspx



  • display date and time