By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,613 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

Converting a date string to a DateTime. Example please

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Kim Hellan" <so*****@nowhere.com> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...
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);
Nov 17 '05 #2

P: n/a
>>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);


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.
Nov 17 '05 #3

P: n/a
Kim,
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?


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
Nov 17 '05 #4

P: n/a
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
Nov 17 '05 #5

P: n/a
>>> 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?


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
Nov 17 '05 #6

P: n/a
"Kim Hellan" <so*****@nowhere.com> wrote in message
news:uc***************@tk2msftngp13.phx.gbl...
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);

Nov 17 '05 #7

P: n/a
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 Hellan wrote:
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?


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


--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #8

P: n/a

"Kim Hellan" wrote...
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
Nov 17 '05 #9

P: n/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" <Ja*********@mvps.org> skrev i en meddelelse
news:ua**************@TK2MSFTNGP10.phx.gbl...
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 Hellan wrote:
> 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?

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


--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Nov 17 '05 #10

P: n/a
"Kim Hellan" <so*****@nowhere.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
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"
Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.