Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i am a student to this programming world
I am doing a csharp project but the problem here i am facing is mask textbox filling with current date. and another mask textbox with current time
like
25/12/2012 and
wit
10:10 AM
i have made this but some error is coming.
maskedTextBox1.Text = DateTime.Now.ToString();
Quote:
but it is filling like this
12/62/012_
how to make this as
06/12/2012
Posted 6-Dec-12 2:37am
Edited 6-Dec-12 4:37am
v2
Comments
Krunal Rohit at 6-Dec-12 7:39am
   
Whats your problem ???
shaikh-adil at 6-Dec-12 8:18am
   
problem?
how can i fill the mask textbox with current date with this format
25/12/2012
ridoy at 6-Dec-12 7:53am
   
which error?you need to mention that..
shaikh-adil at 6-Dec-12 8:16am
   
its of bool conversion. but can you suggest answer how to fill masked textbox with 25/12/2012 this type of date
ryanb31 at 6-Dec-12 8:15am
   
What's the error?
shaikh-adil at 6-Dec-12 8:17am
   
dont concentrate on error sir.
can you suggest how to fill mask textbox with date?
ryanb31 at 6-Dec-12 8:18am
   
Sure. maskedTextBox1.Text = DateTime.Now.ToString();
shaikh-adil at 6-Dec-12 8:36am
   
it fills the msktextbox with 12/62/012_ how to make this
06/12/2012
ryanb31 at 6-Dec-12 8:38am
   
Sounds like your culture is set differently.
shaikh-adil at 6-Dec-12 8:53am
   
sorry if i am disturbing you but
what to set then?
ryanb31 at 6-Dec-12 8:54am
   
Is it ASP.Net? If so, you set the Culture on the current thread. That way DateTime.Now will come out in the format you want.
shaikh-adil at 6-Dec-12 9:01am
   
nope. it is winform :(
ryanb31 at 6-Dec-12 9:05am
   
Well, it's the same concept. I haven't done winforms in years but I'm sure it likely the same thing. Look at http://stackoverflow.com/questions/8952400/how-to-set-application-wide-cultural-information-in-a-winform-application and see if that helps.
shaikh-adil at 6-Dec-12 9:32am
   
nope sir i didint understood.
i dont know how to do it
ryanb31 at 6-Dec-12 9:38am
   
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-CA")
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo("en-CA")
 
but just put in whatever culture you want.
shaikh-adil at 6-Dec-12 9:45am
   
but what is this culture???
i am hearing this first time sir
ryanb31 at 6-Dec-12 9:57am
   
You said your date is going in with Month first and you want to make the day be first, right? You want dd-mm-yy instead. So, set the culture to some culture that supports that.
shaikh-adil at 6-Dec-12 10:06am
   
ohkay. i got you thanks sir
ahmad zrein at 6-Dec-12 9:00am
   
maskedTextBox1.Text = convert.todatetime(DateTime.Now.ToString());
shaikh-adil at 6-Dec-12 9:17am
   
Cannot implicitly convert type 'System.DateTime' to 'string"
this error is coming
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this:
 
  maskedTextBox1.Text = string.Format("{0:dd/MM/yyyy}", DateTime.Now);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

maskedTextBox1.Text = Convert.ToDateTime(DateTime.Now).ToString();
  Permalink  
Comments
shaikh-adil at 6-Dec-12 9:40am
   
by your code
this is the output 12/62/012_
how to make this as
06/12/2012
CHill60 at 6-Dec-12 9:51am
   
Works fine for me - What is the Mask property on your text box set to?
shaikh-adil at 6-Dec-12 10:01am
   
the mask propoerty is set to short date
i.e
00/00/0000
shaikh-adil at 6-Dec-12 10:05am
   
sir can you comment and solve my problem
i have a discussion here in comment. just take a look of comment in this page please
http://www.codeproject.com/Questions/503496/showingplusimageplusonplustab
CHill60 at 6-Dec-12 10:38am
   
You could try maskedTextBox1.Culture = new CultureInfo("en-GB"); prior to putting the data into the maskedtextbox (you will need to use System.Globalization namespace).
 
Silly question from me ... does it absolutely have to be a *masked* text box - could you just use an ordinary text box and format the date e.g. textBox1.Text = DateTime.Now.ToString("dd/MM/yyyy");
shaikh-adil at 6-Dec-12 11:05am
   
yess you are right but i prevoiusly thought about maskedtextbox and for editing purpose mask textbox is good rather using textbox.
when you erase the text in simple textbox it erases all the things and in masked textbox it olny erases the digits. it is less occurency of error that a user misunderstood to enter the text in masked textbox than the textbox
CHill60 at 6-Dec-12 11:08am
   
Perhaps a DatePicker might be better then
I like BC @ CV's solution below as well
phil.o at 6-Dec-12 11:49am
   
Convert.ToDateTime(DateTime.Now)
=> do you really understand what this command is doing ?
You convert a DateTime to a DateTime... sorry, but this is simply useless :)
CHill60 at 6-Dec-12 11:55am
   
thanks for spotting the typo - I was actually intending to correct ahmad zrein's comment and got distracted by some real work. Ironically it works so it's not completely useless ;-p
phil.o at 6-Dec-12 12:13pm
   
Yes it is :)
The correct way in this particular case would be maskedTextBox1.Text = DateTime.Now.ToString();
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

in the form right click properties
 
custom format yyyy/MM/dd
  Permalink  

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

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100