using c#3.5, how do I take a string which contains a data, and format it as
a short data format like this:
"2/5/2008 1:17:11 AM"
and format to:
"02/05/2008"
Thanks.

Signature
moondaddy@newsgroup.nospam
Peter Duniho - 11 Feb 2008 18:08 GMT
> using c#3.5, how do I take a string which contains a data, and format it
> as
[quoted text clipped - 5 lines]
>
> "02/05/2008"
Well, one way would be to use the DateTime.TryParse() method to convert
the string to a DateTime object, and then use the
DateTime.ToShortDateString() to reformat the data into the the string you
want.
Pete
Cor Ligthert[MVP] - 11 Feb 2008 18:17 GMT
MoonDaddy,
First check the globalization settings are US then just
\\\
var date = DateTime.Parse("5/2/2008 1:17:11").ToShortDateString();
///
I have done it in my globalisation settings.
Cor
Nicholas Paldino [.NET/C# MVP] - 11 Feb 2008 18:20 GMT
Well, first you would parse the original string into a DateTime
instance. I would call TryParse, passing the general date time pattern "g"
(assuming that your culture is "en-us").
Because the generat date time pattern is dependent on the current
culture information to determine the format of the general date/time
pattern, if you want to use an exact pattern, you would use:
d/M/yyyy h:mm:ss tt
Of course, even the above is subject to culture info, in that the AM/PM
designator is specific to the DateTimeFormatInfo on the current culture.
You would probably want to make sure that the invariant culture is used when
parsing.
Then, once you have the DateTime instance, you can call ToString on it,
passing a format of:
dd/MM/yyyy
For your output.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> using c#3.5, how do I take a string which contains a data, and format it
> as a short data format like this:
[quoted text clipped - 6 lines]
>
> Thanks.
Ignacio Machin ( .NET/ C# MVP ) - 11 Feb 2008 18:27 GMT
Hi,
You have to convert the string to DateTime and then use
DateTime.ToShortDateString();
Take a lookat DateTime.TryParse or Convert.ToDateTime

Signature
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
> using c#3.5, how do I take a string which contains a data, and format it
> as a short data format like this:
[quoted text clipped - 6 lines]
>
> Thanks.