Adding the data/time onto my form ?

martynball

Member
Joined
Jul 8, 2014
Messages
14
Programming Experience
Beginner
Ive been looking on Google but can't seem to find a way to add the date and the time to my form. I was assuming that I would need a timer to keep updating a label with the current date and time right ?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,141
Location
Sydney, Australia
Programming Experience
10+
The Timer is not specifically about the date and time. Like every problem, break it up into smaller parts.

First, display the current date and time. That has nothing to do with a Timer. You can do that on the Load of the form or the Click of a Button. The DateTime.Now property will give you a DateTime value containing the current date and time. You can call ToString on that and pass an appropriate format specifier to create a String in the format you want. MSDN has all the information you need about date and time format strings. You can then display that String however you see fit, e.g. by assigning it to the Text of a Label.

Once you can display the current date and time, then you'll want to update it regularly so that it remains current. That's where the Timer comes in. You set the Interval appropriately so that the Tick event is raised on the schedule that you want, e.g. once every second. You can then put whatever code that you might put in another event handler, e.g. the Load of the form or the Click of a Button, to have that code executed regularly on that schedule.
 
Top Bottom