datetime

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
H

Hrvoje Voda

How to convert text from textbox "12.05.1977" into "12/05/1977"? and also
check if the text is in right format before

converting?

Hrcko
 
Hrvoje Voda said:
How to convert text from textbox "12.05.1977" into "12/05/1977"? and also
check if the text is in right format before

converting?

Hrcko



string strDate = "12.05.1977";
strDate = strDate.Replace( '.', '/' );
DateTime dt;
bool b = DateTime.TryParse( strDate, out dt );
if( b ) // correct date format
{
}
 
Hrvoje said:
How to convert text from textbox "12.05.1977" into "12/05/1977"? and also
check if the text is in right format before converting?

What type do you want the data to be in when you're done? Your post
literally taken is asking to reformat a string into a different string.
Is that what you want? Or do you really want a DateTime object when
you're done?

If you want a string, one possibility is to use the String.Replace()
method, as suggested in Ashot's post (but of course you wouldn't bother
with the DateTime.Parse() call in his code). Of course, that wouldn't
check the format for you.

If you want a DateTime, then IMHO it is pointless to do any string
conversion first (including the call to String.Replace() that Ashot
suggests). Assuming the input string must be in the format you've
provided, this code will check the format and convert at the same time:

DateTime dt;
string strInput = "12.05.1977";

if (DateTime.TryParseExact(strInput, "MM.dd.yyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
// String is a correctly formatted DateTime, and
// the value is now in the dt variable.
}

If you want a string, AND you do want to verify that the original string
is parse-able as a DateTime, then I would actually use the above, and
then inside the if() statement's block, include this:

string strOutput = dt.ToString("MM/dd/yyyy");

You could of course do some explicit checking of the format yourself,
and doing so could even have better performance (especially since the
format conversion at that point is a simple search-and-replace, rather
than a full DateTime-to-string conversion), but it seems to me that the
DateTime class formatter will do just that, with easy-to-read, simple code.

Pete
 
I get an error for CultureInfo.InvariantCulture and DateTimeStyles.None

What namespace should I use for this property?

Hrcko
 
Hrvoje said:
I get an error for CultureInfo.InvariantCulture and DateTimeStyles.None

What namespace should I use for this property?

Well, assuming you don't just want to use Visual Studio's built-in
"Resolve" feature, you can manually add the System.Globalization
namespace to your using directives.

Personally, I prefer to just let Visual Studio tell me what namespace I
need. :) It works fine, as long as the namespace is in an assembly
your project already references.

Pete
 
I get an error for CultureInfo.InvariantCulture and DateTimeStyles.None

What namespace should I use for this property?

It's not the property - it's probably the class.

The best thing to do when you don't know a namespace is to look up the
type in MSDN.

In this case, both CultureInfo and DateTimeStyles are in
System.Globalization.

Jon
 
It's not working.
If clause does not pass.


Jon Skeet said:
It's not the property - it's probably the class.

The best thing to do when you don't know a namespace is to look up the
type in MSDN.

In this case, both CultureInfo and DateTimeStyles are in
System.Globalization.

Jon
 
Code:
DateTime dt;


if (DateTime.TryParseExact(txtDatumRod.Text, "MM.dd.yyyy",

CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))

{

txtDatumRod.Text = dt.ToString("MM/dd/yyyy");

}



Hrcko
 

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

Back
Top