index > Visual C# General > Different ShortDateString Format with Windows

Different ShortDateString Format with Windows

I have a windows service that writes to a .txt file DateTime.Now.ToShortDateString(),
the windows service wrote 7/21/2006, but when I use another program to print the DateTime.Now.ToShortDateString() using message box, it prints 7-21-2006.

I created that windows service about 2 weeks ago and I changed the date time format last week. I tried rebuilding the windows service, then re-install it, still got the same date format. I added some new functionality to the windows service, the functionality worked, but I still get the same format. Is there any way to solve this problem?

thanks
edwinzzz
It is not a problem. The format used to present dates in string format using ToShortDateString() is based on the current thread culture. If you change the current thread culture, by code or by settings, you change the way the ToShortDateString() ouput looks.

If you want to keep it in your own hands, define a date formatting string that formats the date the way you want it, and use that one in a Date.ToString() or set the current culture to the culture you want in code.


Mark the best replies as answers. - http://bloggingabout.net/blogs/rick
Rick van den Bosch
Guessing a bit, but a custom short-date format is a per-user setting and the service uses a different user account. Override the default short-date format with DateTime.ToString("M-d-yyyy")
nobugz

another thought the  ToShortDateString() is formmating acording to the the pattern ShortDatePattern property of current thread culture

so you can change the formmating by write some thing like

CultureInfo ci = new CultureInfo("en-US",false);

//ci.DateTimeFormat.DateSeparator="-";

//For example

ci.DateTimeFormat.ShortDatePattern = "m-d-yyyy";

Thread.CurrentThread.CurrentCulture = ci;




MCAD
Mohammad Al Husseiny
reply 4

You can use google to search for other answers

 

More Articles

• web user control
• Computer LDAP path
• Listview, View -- Detail , change color
• "The parameter 'sectionGroupName' is invalid"
• how to detect internet connection automatically
• image in user control
• how do i add a child node to a particular parent node?
• The best way to clip and store an image
• .net Windows Service crash
• DateTime result
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• windows application
• access a variable or control
• Email messages displayed without the HTM
• Can't zip nested folders
• Refection question
• App.config: I can not read from <appl
• Image processing: question!
• Strong type collection
• How do I get the number of nodes selecte
• Datagridview delete row
• How Can I Create File or Folder on [Map
• Stored procedure in C#
• Need code, please
• how to change two rows index
• ApplicationContext and Windows Shutdown

Hot Articles

• Is there a way to read text contained in
• TypeConverter
• how to create .ico file?
• save richtext in database
• How do I retrieve distinct DataRows from
• CACHING
• Array of TextBox !!!
• Get data from XML Auto
• comboboxcolumn in datagridview
• Session or viewstate, default value
• Is this code thread safe?
• how to dynamiclly use generic class in s
• How to get the handle of a window which
• How to read big text file fast ?
• simple xml parsing with csharp (debugging

Recommend Articles

• Deleting IE Cache
• How to draw a triangle?
• Creating and Displaying graphs and chart
• Creating a Paint app.
• make setup
• Question on Winows Service using c#
• How to dynamically Create huge grid of e
• Stored procedures Help in C#
• Using a Timer
• Msn Alert
• Setup Set Version and Install Overwrite
• Pasting code from Dreamweaver into Visua
• Registry Access
• Transferring data from one form to anoth
• C# and the __LINE__ or __FILE__ macros