Date formatting issue
-
I'm using
DateTime.Now.ToString("M/d/yyyy")
to try and format today's date. I've noticed that when I change my regional settings through control panel, it changes the result I get from this method. When I set my default date format to dd-MMM-yy in Control Panel, it changes the output of my ToString method from containing slashes (as I've specified in my format string) to dashes, which I can only assume are coming from my regional settings. I thought the point of specifying my own format string was so that I could, oh I dunno, specify my own format?! Are my control panel settings really affecting this method call? Why in the heck? I realize I can just code my own cheesey routine to format dates and avoid the issue, but I'd like to understand what's going on here.
-
I'm using
DateTime.Now.ToString("M/d/yyyy")
to try and format today's date. I've noticed that when I change my regional settings through control panel, it changes the result I get from this method. When I set my default date format to dd-MMM-yy in Control Panel, it changes the output of my ToString method from containing slashes (as I've specified in my format string) to dashes, which I can only assume are coming from my regional settings. I thought the point of specifying my own format string was so that I could, oh I dunno, specify my own format?! Are my control panel settings really affecting this method call? Why in the heck? I realize I can just code my own cheesey routine to format dates and avoid the issue, but I'd like to understand what's going on here.
Hi, a typical mistake. This page of MSDN documentation[^] tells you what the parts of the format string represent; M is an indicator for the month number, and / for the date separator. As a result regional settings are still infiltrating your app! You could create your own DateTimeFormatInfo object and set all of its details any way you like, then pass that to DateTime.ToString() instead of a string. [ADDED] Or you could single quotes to escape the special characters you want to be taken literally, so use '/' to get a real slash, no matter what. [/ADDED] :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
modified on Friday, December 25, 2009 3:46 PM
-
Hi, a typical mistake. This page of MSDN documentation[^] tells you what the parts of the format string represent; M is an indicator for the month number, and / for the date separator. As a result regional settings are still infiltrating your app! You could create your own DateTimeFormatInfo object and set all of its details any way you like, then pass that to DateTime.ToString() instead of a string. [ADDED] Or you could single quotes to escape the special characters you want to be taken literally, so use '/' to get a real slash, no matter what. [/ADDED] :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
modified on Friday, December 25, 2009 3:46 PM
Well I'll be, thanks.
-
Well I'll be, thanks.
You're welcome. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages