DateTime.ToString() lies

  • Thread starter Thread starter vooose
  • Start date Start date
V

vooose

Consider...

Console.WriteLine("date=" + DateTime.Now.ToString("d/MM/yyyy"));

On XP I see what you'd expect but on Windows 2003 I see 14-10-2005!
Anyone know what is going on?
 
It sure is. It has created a bug down the track because a regex is
expecting / as a delimiter and not -
 
Just curious, why are you using regex to parse apart datetimes? Why not
just use the static Parse method on the DateTime structure?
 
Hi Nicholas

The date string is combined with several other strings to form a
key..this key is then used by a regex. This wasn't actually coded by me
it's been around for some time but have discovered this ToString()
difference just now
 
For the record, the following works on 2003

Console.WriteLine("date=" + DateTime.Now.ToString("d'/'MM'/'yyyy"));

gives the expected outcome. But its inconsistent, you should be forced
to do in on XP or it should work without the ' on 2003.
 
vooose said:
Consider...

Console.WriteLine("date=" + DateTime.Now.ToString("d/MM/yyyy"));

On XP I see what you'd expect but on Windows 2003 I see 14-10-2005!
Anyone know what is going on?

Yes - it's doing exactly what it's meant to do. "/" is the date
separator character - it's replaced with whatever the date separator in
that culture is.
 
vooose said:
For the record, the following works on 2003

Console.WriteLine("date=" + DateTime.Now.ToString("d'/'MM'/'yyyy"));

gives the expected outcome. But its inconsistent, you should be forced
to do in on XP or it should work without the ' on 2003.

It's not inconsistent - it just depends on what the
DateTimeFormatInfo.DateSeparator is.
 
Back
Top