rima66345

hello

can any one help me to converter a stringdate with this format "Mon Nov 21 11:34:17 2005"

in date like dateTime=date.parse("stringdate")

thanks




Re: Visual C# General convert a string to date but whith new format

TaylorMichaelL

Assuming that you can't rely on the locale formatting then you'll need to explicitly lay out the formatting through the ParseExact method.

DateTime dt = DateTime.ParseExact(str, "MMM d HH:mm:ss yyyy", null, DateTimeStyles.AllowInnerWhite | DateTimeStyles.AllowWhiteSpaces);

Unfortunately I don't believe DateTime will be able to handle the weekday name so you'll need to strip it off first.

Michael Taylor - 2/7/06





Re: Visual C# General convert a string to date but whith new format

rima

i use c# 2005 and DateTimeStyles does not existes

if you have the equivalente tel me please

thanks






Re: Visual C# General convert a string to date but whith new format

TaylorMichaelL

DateTimeStyles is available in the System.Globalization namespace. It is part of the .NET framework so you should have it.

Michael Taylor - 2/7/06





Re: Visual C# General convert a string to date but whith new format

footballism

Using DateTime.TryParseExact() method rather than DateTime.ParseExact() method can improve performance, since the former won't throw an exception when the parse fails.

Sheva





Re: Visual C# General convert a string to date but whith new format

heff89

string tem = "04/11/06 04:51";
string
format = "MM/dd/yy HH:mm";
bool er = !DateTime.TryParseExact(tem, format, null,
DateTimeStyles
.AllowInnerWhite | DateTimeStyles.AllowWhiteSpaces, out newdate);

Could someone please tell me why er returns 'true'. Thank you





Re: Visual C# General convert a string to date but whith new format

heff89

string tem = "04/11/06 04:51";
string format =
"MM/dd/yy HH:mm";
DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.DateSeparator =
"/";
dtfi.TimeSeparator =
":";
er = !
DateTime.TryParseExact(tem, format, dtfi,
DateTimeStyles.AllowInnerWhite | DateTimeStyles.AllowWhiteSpaces, out newdate);

HAHAHAHA! SUCCESS! FINALLY! The datetimeformatinfo class is a great thing