Click here to Skip to main content
12,998,875 members (64,956 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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
25/12/2012 and
10:10 AM
i have made this but some error is coming.
maskedTextBox1.Text = DateTime.Now.ToString();

but it is filling like this
how to make this as
Posted 6-Dec-12 1:37am
Updated 6-Dec-12 3:37am
Krunal Rohit 6-Dec-12 7:39am
Whats your problem ???
shaikh-adil 6-Dec-12 8:18am
how can i fill the mask textbox with current date with this format
ridoy 6-Dec-12 7:53am
which error?you need to mention that..
shaikh-adil 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 6-Dec-12 8:15am
What's the error?
shaikh-adil 6-Dec-12 8:17am
dont concentrate on error sir.
can you suggest how to fill mask textbox with date?
ryanb31 6-Dec-12 8:18am
Sure. maskedTextBox1.Text = DateTime.Now.ToString();
shaikh-adil 6-Dec-12 8:36am
it fills the msktextbox with 12/62/012_ how to make this
ryanb31 6-Dec-12 8:38am
Sounds like your culture is set differently.
shaikh-adil 6-Dec-12 8:53am
sorry if i am disturbing you but
what to set then?
ryanb31 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 6-Dec-12 9:01am
nope. it is winform :(
ryanb31 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 and see if that helps.
shaikh-adil 6-Dec-12 9:32am
nope sir i didint understood.
i dont know how to do it
ryanb31 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 6-Dec-12 9:45am
but what is this culture???
i am hearing this first time sir
ryanb31 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 6-Dec-12 10:06am
ohkay. i got you thanks sir
ahmad zrein 6-Dec-12 9:00am
maskedTextBox1.Text = convert.todatetime(DateTime.Now.ToString());
shaikh-adil 6-Dec-12 9:17am
Cannot implicitly convert type 'System.DateTime' to 'string"
this error is coming
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try this:

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

Solution 1

maskedTextBox1.Text = Convert.ToDateTime(DateTime.Now).ToString();
shaikh-adil 6-Dec-12 9:40am
by your code
this is the output 12/62/012_
how to make this as
CHill60 6-Dec-12 9:51am
Works fine for me - What is the Mask property on your text box set to?
shaikh-adil 6-Dec-12 10:01am
the mask propoerty is set to short date
shaikh-adil 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
CHill60 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 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 6-Dec-12 11:08am
Perhaps a DatePicker might be better then
I like BC @ CV's solution below as well
phil.o 6-Dec-12 11:49am
=> do you really understand what this command is doing ?
You convert a DateTime to a DateTime... sorry, but this is simply useless :)
CHill60 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 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
Please Sign up or sign in to vote.

Solution 3

in the form right click properties

custom format yyyy/MM/dd

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2017
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