parsing date string

H

Herfried K. Wagner [MVP]

Cor Ligthert said:
I have to say that my simple short solution does not work .

So it has to be with CDate when in Israel US settings are used
\\\
Thread.CurrentThread.CurrentCulture = New CultureInfo("FR-fr")
mydatarow("dtt") = CDate("19/10/2004 0:15:51")
Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture
///

In addition to the other replies: Why play around with cultures, if the OP
wants to solve a culture-invariant problem?
 
H

Herfried K. Wagner [MVP]

Jon Skeet said:
I just wanted to make the point that DateTime.ParseExact
shouldn't be regarded as an entirely culture-insensitive method.

ACK. That's what the documentation on this method says too...

:)
 
C

Cor Ligthert

Herfried,
That does CDate as well and even in one time, without knowing anything of
globalization settings.

That's wrong.

Did you read that in my message, I have told that already 2 hours ago.

Cor
 
G

Guest

.....But lets take a look at exactly HOW CDate works under the hood.

The CDate keyword ends up compiling into this (I'll translate it into VB):

Public Shared Function CDate(value as String) as DateTime

Return
Microsoft.VisualBasic.CompilerServices.DateType.FromString(value,
Thread.CurrentThread.CurrentCulture)

End Function

(Yeah, I know.... DateType.FromString implementation ends up delegating the
actual parsing to the DateTime.Parse(string, provider, styles) method. The
important thing here is where the culture information is derived)

So, if I'm writing a web application where the aspnet process, on my US
computer is parsing a string date that a person in another culture entered,
CDate would parse that text using the executing thread's culture
settings...which might be bad, so doing a ParseExact, using the client's
culture settings would be appropriate in that case.
 
C

Cor Ligthert

David,

See my sample what I made today and is in the top of the thread, your
example is exactly why I think setting the culturesetting is better.

While I started for this thread with the idea that it was not needed because
it is Isreal which culture is very much binded with Europe (For world sport
and cultural organisations is Isreal the same as Turkey and Russia a part of
Europe while the biggest part and Isreal complete of those countries are in
Asia).

The problem could be that I was wrong in that idea, therefore I asked the OP
for that.

In the first situation he was needing to set the right culture datetime
setting from his system.

In the second situation that datetime.parseexact with globalization setting
or whatever way to convert however not only the Cdate, in what I made a
stuppid mistake.

Cor







Cor
 
G

Guest

Have you tried the following? It works great.

DateTime.Today.ToString("MM/dd/yyyy hh:mm:ss");

Thanks
Nital
 
J

Jon Skeet [C# MVP]

Nital said:
Have you tried the following? It works great.

DateTime.Today.ToString("MM/dd/yyyy hh:mm:ss");

That converts from a DateTime to a string, rather than the other way
round. (It also has the month and day the wrong way round as far as the
OP's original format is concerned, and doesn't use 24 hour format for
the hours.)
 
G

Guest

Hi
Try to use system.globalization (IFormatter) to suite your date string format.
I had the same problem with yyyy/mm/dd string which is in Japnese format.
I used the IFormatter to set the locale and then my strig was recognized in
the Convert.DateString(String) function.
Let me know if this works
 
G

Guest

HI,

I also had a problem with datetime. Searched a lot on the net. Read through
a lot of articles. But, none was of any help.

At Last i found an MSDN article which explains the datetime class. it solved
my problem. its a very good article.

The link of this article is
http://msdn.microsoft.com/netframework/programming/bcl/faq/DateAndTimeFAQ.aspx.

After going through the article i found that the best way is the following :

Date.Today.ToString( System.Globalization.CultureInfo.InvariantCulture )

Hope it solves all of your problems and help you in understanding the
DateTime class.
 
H

Herfried K. Wagner [MVP]

BabyEmperor said:
I also had a problem with datetime. Searched a lot on the net. Read
through
a lot of articles. But, none was of any help.

At Last i found an MSDN article which explains the datetime class. it
solved
my problem. its a very good article.

The link of this article is
http://msdn.microsoft.com/netframework/programming/bcl/faq/DateAndTimeFAQ.aspx.

After going through the article i found that the best way is the following
:

Date.Today.ToString( System.Globalization.CultureInfo.InvariantCulture )

Hope it solves all of your problems and help you in understanding the
DateTime class.

Whose problem?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top