views:

67

answers:

3

Guys,

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?

Thx

+4  A: 

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.

String date = String.Format("{0:s}", currTime);

This can be shorted a bit to :

String date = currTime.ToString("s");
James Curran
Hmmm.. I typed `DataTime` *twice* ?? See what happens when I don't have Intellisense...
James Curran
+1  A: 

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);

use

var message = string.Format("The parcel was sent on {0}.", currTime.ToString("s"));
Timwi
+2  A: 

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.

fearofawhackplanet
Would the downvoter be so kind as to explain what is wrong with this answer?
fearofawhackplanet
I have no idea, take a +1 from me to counteract it.
Rob