Take the 2-minute tour ×

Im working with VS2008, .NET and C#, and I need to send to one of our clients a DATETIME variable.

The problem is that they want the Date in the format Sortable date/time pattern ("s").

When I get the actual datetime, it is a Datetime object. When I format it to the given format is now a String object, and it has the format I want. But after that I can't create a Datetime object from that formatted String with the same format, because it always returns it to the original Datetime format.

More specific:

DateTime currTime = System.DateTime.Now; //(the format is "13/08/2010 09:33:57 a.m.")

String date = String.Format("{0:s}", currTime);// (wanted format "2010-08-13T09:33:57")

DateTime newDate = DateTime.Parse(date);// (original format again "13/08/2010 09:33:57 a.m.")

IFormatProvider culture = new System.Globalization.CultureInfo("", true); //(Invariant Culture)
String format = "s";                        
DateTime fecha = DateTime.ParseExact(date, format, culture); // (original format again "13/08/2010 09:33:57 a.m.")

Is there a way of getting a Datetime object with the desired format, or Datetime objects use a given format, and you can't format them to the equivalent string formats?


share|improve this question

3 Answers 3

up vote 5 down vote accepted

If I understand the question correctly, I think you are getting confused. A DateTime object itself is not formattable, it is essentialy just a numeric value (number of ticks since DateTime.MinValue or whatever it is).

You can convert a DateTime object into a string representation in whatever format you like, but you aren't changing the actual DateTime object.

share|improve this answer
Would the downvoter be so kind as to explain what is wrong with this answer? –  fearofawhackplanet Aug 13 '10 at 14:56
I have no idea, take a +1 from me to counteract it. –  Rob Aug 13 '10 at 15:05

A DateTime is just a number. It has no intrinsic "format". It is only rendered into a format when converted to a string. Hence, whenever you need a DateTime as a string, you have to specify what format you want it in.


This can be shorted a bit to :

String date = currTime.ToString("s");
share|improve this answer
Hmmm.. I typed DataTime twice ?? See what happens when I don't have Intellisense... –  James Curran Aug 13 '10 at 15:04
After executing following syntax String date = currTime.ToString("s"); output will be 2014-01-29T11:25:29 Now can you save this dateTime format into a variable having Type DateTime ? –  Ashish Jain Jan 29 at 5:56

Every time you use a DateTime value in a place where it needs to be turned into a string (e.g. in string.Format()), C# will generally call the .ToString() method. The DateTime type declares a .ToString() method that has the format you don’t want.

However, DateTime has additional methods, including .ToString(IFormatProvider provider) and .ToString(string format).

Therefore, you can probably achieve what you want if you replace every use of a DateTime variable in the relevant string-like context to one that calls the appropriate .ToString overload, for example:

Instead of

var message = string.Format("The parcel was sent on {0}.", currTime);


var message = string.Format("The parcel was sent on {0}.", currTime.ToString("s"));
share|improve this answer

Your Answer


