change the date format dd/mm/yyyy to m/d/yy

C

Claudia Fong

Hi

Is there a way to change a date format dd/mm/yyyy to m/d/yy?

I have a textBox where the user should put a date, but no matter what
format the user input dd/mm/yyyy or m/d/yy, I need to convert it to
this format: m/d/yy.


Cheers!

Claudi
 
D

Dan Bass

how do you know whether they mean "dd mm yyyy" or "mm dd yyyy"? On friday it
will be the 1st of the 7th, but also the 07 / 01 according to the US.
Resolving date ambiguity is something many people miss...
This problem could be solved, for example, with drop downs stating "Day[ ]
Month[ ] Year[ ]" which contain values you populate so that the user has no
choice. The other way would be to have a readonly text box and for the user
to choose the date through a calendar control.

DateTime.ParseString() (i think it is) will take in your string and convert
it to a DateTime object, from there you can do what you want with it, then
set it back to a string by
myDateTimeObject.ToString ("dd/mm/yyyy" );

Good luck.

Dan.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Then you have two problems:
1- First you have to check the Text entered and decide in which format it's
written and also of course if it;s valid in either one.
solution: Use a regular expression for this.
2- Using DateTime.ParseExact like this:
DateTime.ParseExact ( textbox1.Text, "M/d/yy" ,
DateTimeFormatInfo.InvariantInfo );


Cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

well in the case of the OP he can decide based on the year, as both months
and days may be two digits, I forgot to mention it in the response I just
sent.

I do concord with you 100% that the ambiguity is a problem, particularly in
this case.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Dan Bass said:
how do you know whether they mean "dd mm yyyy" or "mm dd yyyy"? On friday
it will be the 1st of the 7th, but also the 07 / 01 according to the US.
Resolving date ambiguity is something many people miss...
This problem could be solved, for example, with drop downs stating "Day[ ]
Month[ ] Year[ ]" which contain values you populate so that the user has
no choice. The other way would be to have a readonly text box and for the
user to choose the date through a calendar control.

DateTime.ParseString() (i think it is) will take in your string and
convert it to a DateTime object, from there you can do what you want with
it, then set it back to a string by
myDateTimeObject.ToString ("dd/mm/yyyy" );

Good luck.

Dan.

Claudia Fong said:
Hi

Is there a way to change a date format dd/mm/yyyy to m/d/yy?

I have a textBox where the user should put a date, but no matter what
format the user input dd/mm/yyyy or m/d/yy, I need to convert it to
this format: m/d/yy.


Cheers!

Claudi
 
C

Chris Dunaway

Rather than use a textbox, why not use a DateTime Picker control? That
way it eliminates the ambiguity and you can take the selected date and
convert to any format you need using the .ToString method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top