Click here to Skip to main content
15,306,142 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
please help about get date and time from system to show in box in the windows form application .
i want to show date in textBox1 and show time in textBox2.
Posted
Comments
[no name] 19-Aug-12 19:51pm
   
Google much? http://msdn.microsoft.com/en-us/library/system.datetime.aspx

C#
Datetime d = new DateTime();
d = DateTime.Now;

TextBox1.Text = d.ToString("dd.MM.yyyy");
TextBox2.Text = d.ToString("HH:mm:ss");


Google would give you the same result, and It would be nice if you tried somethin gyourself, instead of posting this non question, were I have to do the work for you ;)
   
v6
Comments
Sergey Alexandrovich Kryukov 19-Aug-12 20:13pm
   
Not Now(), but Now (this is a property). Did you try to compile it? It's useful to give answers correctly...
--SA
Kenneth Haugland 19-Aug-12 20:21pm
   
Yes, think im off to bed now... Too late for me :)
Seen your answer, he could ofcoures use a Label if he just want to show a date and not being able to edit it.
Sergey Alexandrovich Kryukov 19-Aug-12 22:58pm
   
Well, there are cases when a read-only text box is better then a label -- it can be selected to copy to the clipboard. In all cases, DateTime picker is better...
--SA
[no name] 19-Aug-12 21:45pm
   
I've always thinking that the experts even sleep? Now I am happy that you are.
+5!
Why showing it in the text box? Text box is designed to input of data as well, but if a user enters time by typing in a text box, it's too easy to make a mistake, and you also need to maintain some particular format or input, inform the user what exactly format is used. So, this is not the best idea.

It's much better to use System.Windows.Forms.DateTimePicker:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker%28v=vs.100%29.aspx[^].

—SA
   
Use just 2lines of code..
C#
textbox2.Text = DateTime.Now.ToString("hh:mm:ss tt");
textbox1.Text = DateTime.Now.ToString("dd MMMM,yyyy");
   
C#
private void Test_Load(object sender, EventArgs e)
        {
            textBox1.Text = DateTime.Now.Date.ToShortDateString();
            textBox2.Text = DateTime.Now.TimeOfDay.ToString();
        }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900