DateTime.ToString

P

Peter Morris

var value = DateTime.MaxValue;
string asString = value.ToString("dd/MM/yyyy HH:mm:ss");
value = DateTime.ParseExact("dd/MM/yyyy HH:mm:ss");

Run this, and I will bet it works, right?

Now (on Vista at least) go to your Windows regional settings and set both
your long and short date format to yyyy-MM-dd and try it again!

ToString uses - instead of / as instructed in the code, causing ParseExact
to fail.

My questions are

01: Does anyone else see this?
02: If so which O/S are you on?
03: Where should I report it?
 
J

Jeff Johnson

var value = DateTime.MaxValue;
string asString = value.ToString("dd/MM/yyyy HH:mm:ss");
value = DateTime.ParseExact("dd/MM/yyyy HH:mm:ss");

Run this, and I will bet it works, right?

Now (on Vista at least) go to your Windows regional settings and set both
your long and short date format to yyyy-MM-dd and try it again!

ToString uses - instead of / as instructed in the code, causing ParseExact
to fail.

My questions are

01: Does anyone else see this?
02: If so which O/S are you on?
03: Where should I report it?

If I remember correctly, I've seen this behavior in VB6's Format function as
well. The issue is that the slash character is not a literal but a
meta-character which stands for "the current date separator." You have to
escape it to make it a "real" slash. Or I could be wrong....

Nope, I'm right. It says so in MSDN:
/ - The date separator defined in the current
System.Globalization.DateTimeFormatInfo.DateSeparator property that is used
to differentiate years, months, and days.
 
P

Peter Morris

Nope, I'm right. It says so in MSDN:
/ - The date separator defined in the current
System.Globalization.DateTimeFormatInfo.DateSeparator property that is
used to differentiate years, months, and days.

But then why isn't ParseExact doing the same thing? So ParseExact must have
a bug then? How do I escape it to be a literal \ ?


Thanks
 
J

Jeff Johnson

But then why isn't ParseExact doing the same thing? So ParseExact must
have a bug then? How do I escape it to be a literal \ ?

A) You don't want a literal \ , you want a literal / .... And you get it
with \/ (that's not a "V"), because the backslash is the escape character.

B) I see what you're saying. Post the actual code you're using for
ParseExact(), because your sample only had a single string argument. In
fact, can you write up a quick sample that we could try in a simple console
app? Mentioning your Framework version would also be a good thing.
 
P

Peter Morris

It was a false alarm. Being distracted by the incorrect separator caused me
to miss the fact that I was setting dd/MM/yyyy into a license, but when it
was later read it was in fact reading MM/dd/yyyy. This was because the
subsequent read operation was on the "license on the hard disk" and not
"license I am generating". The license was generated on a machine that had
its date format set to US format (despite being in the UK), so I was getting
a month that was too high.
But you're right...without a concise-but-complete code sample that
reliably demonstrates the problem, it's not possible to make any definite
observations.

I agree to an extent. In this case the code sample was incorrect because I
copy/pasted it from a conversation in IRC rather than directly from my
source code, and it was incorrect. Running the code in a console app
revealed that it worked, it was only as I compared the two watch windows
side by side that I realised the 12-13 and 13-12 were the opposite way
around.
 
G

Göran Andersson

Peter said:
var value = DateTime.MaxValue;
string asString = value.ToString("dd/MM/yyyy HH:mm:ss");
value = DateTime.ParseExact("dd/MM/yyyy HH:mm:ss");

Run this, and I will bet it works, right?

No, it won't even compile. ;)

The ParseExact method also needs the input string and the culture to use:

DateTime value = DateTime.MaxValue;
string asString = value.ToString("dd/MM/yyyy HH:mm:ss");
value = DateTime.ParseExact(asString, "dd/MM/yyyy HH:mm:ss",
CultureInfo.CurrentCulture);
Now (on Vista at least) go to your Windows regional settings and set
both your long and short date format to yyyy-MM-dd and try it again!

That's what I have already.
ToString uses - instead of / as instructed in the code, causing
ParseExact to fail.

My questions are

01: Does anyone else see this?

Yes, if I specify CultureInfo.InvariantCulture in the ParseExact call,
so that they use different cultures.

If you use literal values instead of the / and : codes, it works
regardless of the culture:

DateTime value = DateTime.MaxValue;
string asString = value.ToString("dd'/'MM'/'yyyy HH':'mm':'ss");
value = DateTime.ParseExact(asString, "dd'/'MM'/'yyyy HH':'mm':'ss",
CultureInfo.InvariantCulture);
02: If so which O/S are you on?
03: Where should I report it?

Nowhere. It works just as expected and as documented. :)
 
A

Arne Vajhøj

Göran Andersson said:
Peter Morris wrote:
DateTime value = DateTime.MaxValue;
string asString = value.ToString("dd/MM/yyyy HH:mm:ss");
value = DateTime.ParseExact(asString, "dd/MM/yyyy HH:mm:ss",
CultureInfo.CurrentCulture);


Yes, if I specify CultureInfo.InvariantCulture in the ParseExact call,
so that they use different cultures.

If you use literal values instead of the / and : codes, it works
regardless of the culture:

DateTime value = DateTime.MaxValue;
string asString = value.ToString("dd'/'MM'/'yyyy HH':'mm':'ss");
value = DateTime.ParseExact(asString, "dd'/'MM'/'yyyy HH':'mm':'ss",
CultureInfo.InvariantCulture);

Another option is to set the DateTimeFormatInfo inside the
CultureInfo.

It is more code, but somehow I think the intention is
more clearly expressed.

Arne
 

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