datetime parsing problem

  • Thread starter Thread starter jai hanuman
  • Start date Start date
J

jai hanuman

this fails.
string sdt="20040330 930";

string[] fmts ={"yyyyMMdd Hmm"};

DateTime date = DateTime.ParseExact(sdt, fmts,

DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.AllowLeadingWhite|DateTimeStyles.AllowTrailingWhite);

any workarounds? urgent, please!!!
TIA
 
jai said:
string sdt="20040330 930";

string[] fmts ={"yyyyMMdd Hmm"};

any workarounds? urgent, please!!!

Why not use an regular expression !?

^(\d\d\d\d)(\d\d\)(\d\d)\s+(\d)(\d\d)$

Now you have
Match.Groups[1] = yyyy
Match.Groups[2] = MM
Match.Groups[3] = dd
Match.Groups[4] = H
Match.Groups[5] = mm

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
Jochen said:
jai said:
string sdt="20040330 930";

string[] fmts ={"yyyyMMdd Hmm"};

any workarounds? urgent, please!!!

Why not use an regular expression !?

^(\d\d\d\d)(\d\d\)(\d\d)\s+(\d)(\d\d)$

Or even better:
^(\d\d\d\d)(\d\d\)(\d\d)\s+(\d?\d)(\d\d)$

So it will also match HH

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
 
jai hanuman said:
this fails.
string sdt="20040330 930";

string[] fmts ={"yyyyMMdd Hmm"};

DateTime date = DateTime.ParseExact(sdt, fmts,

DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.AllowLeadingWhite|DateTimeStyles.AllowTrailingWhite);

any workarounds? urgent, please!!!

The problem (I believe) is that it's greedily taking the hours (as 93).
If you separate the hours from the minutes, or use a 24 hour format, it
should be okay. Is that a possible solution for you?
 
Jon Skeet said:
jai hanuman said:
this fails.
string sdt="20040330 930";

string[] fmts ={"yyyyMMdd Hmm"};

DateTime date = DateTime.ParseExact(sdt, fmts,

DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.AllowLeadingWhite|DateTimeStyles.AllowTrailingWhite);

any workarounds? urgent, please!!!
The problem (I believe) is that it's greedily taking the hours (as 93).
If you separate the hours from the minutes, or use a 24 hour format, it
should be okay. Is that a possible solution for you?

Indeed. Either of the suggestions above (either ParseExact("20040330
0930", "yyyyMMdd Hmm")
or ParseExact("20040330 9 30", "yyyyMMdd H mm")) should succeed.

I have filed a bug on the issue.

Katy King
 
Jon Skeet said:
jai hanuman said:
this fails.
string sdt="20040330 930";

string[] fmts ={"yyyyMMdd Hmm"};

DateTime date = DateTime.ParseExact(sdt, fmts,

DateTimeFormatInfo.InvariantInfo,
DateTimeStyles.AllowLeadingWhite|DateTimeStyles.AllowTrailingWhite);

any workarounds? urgent, please!!!

The problem (I believe) is that it's greedily taking the hours (as 93).
If you separate the hours from the minutes, or use a 24 hour format, it
should be okay. Is that a possible solution for you?

thanks, i had to hack it and add the 0 before the hour before parsing it.
 
Back
Top