Converting a date string to a DateTime. Example please

  • Thread starter Thread starter Kim Hellan
  • Start date Start date
K

Kim Hellan

I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
convert to a DateTime value.
I know this is a standard problem, but please don't just link to all the
MSDN pages regarding Parse() and ParseExact().
I've been there and read all the information about using IFormatProviders,
DateTimeStyles, CultureInfo.......

Bottom line is that I can't get it working, so could someone please just
post 5-10 lines of working code that is needed to perform this simple
conversion?

Thank You,
Kim
 
I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
convert to a DateTime value.

No problem.
I know this is a standard problem, but please don't just link to all the
MSDN pages regarding Parse() and ParseExact().

Er, well that's where the answer lies...
I've been there and read all the information about using IFormatProviders,
DateTimeStyles, CultureInfo.......

Really? Maybe you were having a bad day...
Bottom line is that I can't get it working, so could someone please just
post 5-10 lines of working code that is needed to perform this simple
conversion?

string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse(strDate);
 
I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to
No problem.


Er, well that's where the answer lies...


Really? Maybe you were having a bad day...


string strDate = "13-10-05 10:19:26";
DateTime dtmDate = DateTime.Parse(strDate);

Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month and
day?
Assuming that the application runs on computers with different Windows
settings for date/time format.
 
Kim,
Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month and
day?

It does not, therefore you should only use the DateTime.Parse (or if you use
VBNet the than easier one for that CDate), if it comes from an input control
as the textbox (and than it should be the installed culture way and go
automaticly).

If you use it on internet, than you should be sure that you have showed the
datetime format mask beside the box.

You can on Internet try to get the location from which it comes (that is not
standard) and than assume the culture, however this is in my opinion real
dangerous, because in by instance in Canada are two complete different date
format paterns used.

I hope this helps,

Cor
 
OOPs

Forgot what I wrote about VBNet, this human hit his head again to the same
stone. (A dutch phrase for "once hitten twice shy"). A donkey does not do
that.

:-)

Cor
 
string strDate = "13-10-05 10:19:26";
It does not, therefore you should only use the DateTime.Parse (or if you
use VBNet the than easier one for that CDate), if it comes from an input
control as the textbox (and than it should be the installed culture way
and go automaticly).

If you use it on internet, than you should be sure that you have showed
the datetime format mask beside the box.

You can on Internet try to get the location from which it comes (that is
not standard) and than assume the culture, however this is in my opinion
real dangerous, because in by instance in Canada are two complete
different date format paterns used.

I hope this helps,

Thank you, but my initial question still stands.
Can anybody give me a C# EXAMPLE of how to parse the "13-10-05 10:19:26"
string.
The string always have the format "DD-MM-YY hh:mm:ss" and the parsing should
work on ANY machine, ANYWHERE in the world, regardless of culture/date
settings on the computer used.

Thank You!
Kim
 
Okay, lets say the date string is: "07-11-05 10:19:26".
How does the simple Parse() method above then know what is year, month and
day?
Assuming that the application runs on computers with different Windows
settings for date/time format.

Apologies - I didn't read your original post well enough.

You're right that the data format is ambiguous as it stands - certainly not
Y2k-compliant.

However, if you're confident that the incoming string will always be in the
format you specified in your OP, the following is guaranteed to work:

string strDate = "13-10-65 10:19:26";
int intCenturyThreshold = 55; // amend as required
string strCentury = Convert.ToInt32(strDate.Substring(6, 2)) >
intCenturyThreshold ? "19" : "20";
int intYear = Convert.ToInt32(strCentury + strDate.Substring(6, 2));
int intMonth = Convert.ToInt32(strDate.Substring(3, 2));
int intDay = Convert.ToInt32(strDate.Substring(0, 2));
int intHour = Convert.ToInt32(strDate.Substring(9, 2));
int intMinute = Convert.ToInt32(strDate.Substring(12, 2));
int intSecond = Convert.ToInt32(strDate.Substring(15, 2));
DateTime dtmDate = new DateTime(intYear, intMonth, intDay, intHour,
intMinute, intSecond);
 
I think your main problem is that your format string is wrong:

DateTime dt = DateTime.ParseExact(
"13-10-05 10:19:26",
"dd-MM-yy hh:mm:ss", null);

Console.WriteLine(dt.ToString("F"));

Prints: Thursday, October 13, 2005 10:19:26 AM
 
...
Can anybody give me a C# EXAMPLE of how to parse the "13-10-05 10:19:26"
string.
The string always have the format "DD-MM-YY hh:mm:ss" and the parsing
should work on ANY machine, ANYWHERE in the world, regardless of
culture/date settings on the computer used.

I believe this should work:


string strDate = "13-10-05 10:19:26";

System.Globalization.DateTimeFormatInfo di =
new System.Globalization.DateTimeFormatInfo();

di.FullDateTimePattern = "dd-MM-yy HH:mm:ss";

DateTime d = DateTime.ParseExact(strDate, "F", di);


// Bjorn A
 
This is exactly what I have been looking for.
So simple, and without all that unnecessary IFormatProvider and CultureInfo
stuff.

THANK YOU!


James Curran said:
I think your main problem is that your format string is wrong:

DateTime dt = DateTime.ParseExact(
"13-10-05 10:19:26",
"dd-MM-yy hh:mm:ss", null);

Console.WriteLine(dt.ToString("F"));

Prints: Thursday, October 13, 2005 10:19:26 AM


Kim said:
Thank you, but my initial question still stands.
Can anybody give me a C# EXAMPLE of how to parse the "13-10-05
10:19:26" string.
The string always have the format "DD-MM-YY hh:mm:ss" and the parsing
should work on ANY machine, ANYWHERE in the world, regardless of
culture/date settings on the computer used.

Thank You!
Kim

--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
This is exactly what I have been looking for.
So simple, and without all that unnecessary IFormatProvider and
CultureInfo stuff.

Provided you never need to know / care which century the date refers to...

E.g. end of the First World War...

"11-11-18 11:00:00"
 
Back
Top