Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2005

Tip: Looking for answers? Try searching our database.

What's up with DateTime.ToString() and the date separator?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PIEBALD - 19 Jan 2005 17:35 GMT
Here's a mystery. (Using WinXP Pro) When the date separator in Control Panel
| Regional and Language options | Customize | Date is set to "/" (slash) the
following lines produce the expected output, but when the date separator is
"-" (hyphen), both statements produce output with hyphens as date separators.

           System.Console.WriteLine ( System.DateTime.Now.ToString (
"yyyy-MM-dd HH:mm:ss" ) ) ;
           System.Console.WriteLine ( System.DateTime.Now.ToString (
"yyyy/MM/dd HH:mm:ss" ) ) ;

It seems to me there's something wrong there. As I want to be able to
specify the separator in the ToString() I have to leave my computer set to
the slash separator.

One reason I need to be able to specify at run time is because some dates
get passed to apps and I want to use ISO8601 formats (using the hyphen) and
other times I need to format a parameter for SCHTASKS (the /SC  parameter
requires the date to use slashes as separators, why is that by the way?)

In my opinion, when the programmer specifies the format string as above, the
date separator setting in control panel should not affect the output. Only
when no format is specified ( also with ToLongDateString() and
ToShortDateString() ) should the control panel setting be used.

Or is there something I'm missing?
Jon Skeet [C# MVP] - 19 Jan 2005 18:04 GMT
> Here's a mystery. (Using WinXP Pro) When the date separator in Control Panel
> | Regional and Language options | Customize | Date is set to "/" (slash) the
[quoted text clipped - 7 lines]
>
> It seems to me there's something wrong there.

There's nothing wrong at all. "/" means "date separator", just as
documented in the custom DateTime format strings page.

If you want to use the exact literal "/", put it in quotes:
"yyyy'/'MM'/'dd HH:mm:ss".

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.