views:

128

answers:

3

What is the standard format for datetime with timezone in a C# ASP.NET application?

Updated to clarify: In ASP.NET I need to check a datetime from a Domino database against a .NET DateTime. The client is asking what would be the best format in which he should provide the datetime with the timezone. Meaning which format is most easily readable by .NET?

+2  A: 

Update - regarding your clarification:

I need to check datetime from a dominos database against asp.net datetime. The client is asking what would be the best format in which he should provide the datetime with the timezone.-meaning which is easily readable by .Net.

.NET supports parsing a wide variety of different formats so as long as it is in a fairly standard unambiguous format you should have no problem parsing it. But don't leave it to chance by relying on the default settings, use ParseExact or TryParseExact.

Check out the following resource for details on constructing the format string:

If you want to suggest something to him, then I would recommend following the ISO 8601 standard, which looks something like this: 2010-05-14T10:04Z. DateTime.Parse can also understand this format without needing to provide a format string.


Answering your original question:

What is the standard format for DateTime with timezone in a C# ASP.NET application?

Default formatting of DateTimes is based on the current locale. If you want a consistant format use either CultureInfo.InvariantCulture or else a specific culture of your choice.

DateTime now = DateTime.Now;
string s = now.ToString(CultureInfo.InvariantCulture);

Result:

"05/14/2010 21:17:35"

Note that the default formatting doesn't include the time zone. You can specify your own formatting though:

string s = now.ToString("yyyy-MM-dd HH:mm:ss zz");

Result:

"2010-05-14 21:21:59 +02"
Mark Byers
A: 

The default representation would be:

MM/DD/YYYY HH:MM:SS (AM/PM)

But that also depends on the CultureInfo specified in the current thread. For a complete listing of options, use this MSDN resource:

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Tejs
A: 

Assuming you meant from a string to a DateTime object, the easiest way would be to use DateTime.Parse("MM/DD/YY HH:mm:ss UTC-OFFSET"), such as "03/01/2009 05:42:00 -5:00". More info is in the Parse method documentation.

Marc Bollinger