views:

127

answers:

6

How do i set the DateTime format centrally so that at any time if I use a dateTime.ToString() in my code I get string in the ISO format(eg:2008-2-19 01:00:00)

+1  A: 

You can add the time format as parameter to the tostring, i always use this for reference

Dim d = DateTime.Parse("2008-2-19 01:00:00")
Assert.AreEqual("2008-2-19 01:00:00", d.ToString("yyyy-M-dd HH:mm:ss"))

http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm

Regards

Iain

Iain
+2  A: 

You should use CultureInfo to control the format when using DateTime.ToString()

EDIT:

Once you have set the CurrentCulture on your Current Thread then try the following:

DateTimeFormatInfo format = Thread.CurrentThread.CurrentCulture.DateTimeFormat; string dateTime = DateTime.Now.ToString(format.FullDateTimePattern);

Barry
System.Threading.Thread.CurrentThread.CurrentCulture (and .CurrentUICulture).
Jason Berkan
The culture is being set but it the dateTime format doesnt change. Check edit above
Mulki
@Mulki - I have edited my answer. Thanks
Barry
Hey Barry...i want to do it without using an overload...like this DateTime.Now.ToString(); This is because there are times when im not stating this in my code...eg: foreach( row in dataTable) {jsonwriter.writestring(row.tostring();}
Mulki
+1  A: 

You can use extension methods to extend datetime and create a ToISOString().

check out http://msdn.microsoft.com/en-us/library/bb383977.aspx on ways to accomplish it. In the extension method you can use parameters to format the string the way you need, then you can use DateTime.ToISOString(); You could also use cultureinfo as Barry said, but I don't know if it will fit your needs.

Gmoliv
An extension with a different name will not help...I need it to be done on dateTime.ToString...This is so that when a datatable is being parsed to text/Json/Xml i want the format to be output in the above mentioned format.
Mulki
+2  A: 

You should update the System.Threading.Thread.CurrentThread.CurrentCulture property.

This affects all DateTime.ToString() in the current thread.

Albin Sunnanbo
Im setting both currentculture and uiculture to the one mentioned in the edit...The culture is being set along with the date format..but the dateTimeObj.ToString() continues to show it in en-us format...is something else required to be done other than just setting culture?
Mulki
A: 

With the help of Albin and Barry's answers iv got the following piece to code to set the Time format centrally in the Global.asax.

using System.Globalization;
using System.Threading;

protected void Application_BeginRequest()
        {
            CultureInfo standardizedCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            standardizedCulture.DateTimeFormat.DateSeparator = "-";
            standardizedCulture.DateTimeFormat.LongDatePattern = "yyyy-MM-dd hh:mm:ss";
            standardizedCulture.DateTimeFormat.FullDateTimePattern = "yyyy-MM-dd hh:mm:ss";           
            standardizedCulture.DateTimeFormat.ShortDatePattern  = "yyyy-MM-dd"; 
            Thread.CurrentThread.CurrentCulture = standardizedCulture;
            Thread.CurrentThread.CurrentUICulture = standardizedCulture;
        } 
Mulki
I believe that LongDatePattern = "s"; or "o"; will also do the job.
Iain Galloway
Ideas derived from 1)http://www.hanselman.com/blog/EnablingEvilOverridingSystemDateTimesDefaultToString.aspx2)http://channel9.msdn.com/forums/TechOff/125490-ISO-8601-date-formats-in-ASPNET/
Mulki
A: 

I think the requested format i.e. DateTIme.Now.ToString() will have a very bad influence on your code readability (and maintainability..). Trying to override a well known behavior with a custom one is bad practice.
What I do consider a good way to use it is like so: DateTIme.Now.ToString(IsDefaultFormat).
Now all you need to do is add an extension method to DateTime Which receives a bool, and if that bool is set to true, returns the DateTime using your "default format"

Oren A
This is for a webservice im developing. I need the default format overridden to be sure im not sending it in any format but a singular standard unambiguous format. Besides the default locale is setting en-US date format which is very confusing for me since en-GB is followed here by the entire dev-team.
Mulki