.NET convert Datetime for formatting Sort date / time pattern ("s");

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

The problem is that they want to have a date in Sort date / time ("s") format.

When I get the actual datetime, this is a Datetime object. When I format it to this format, now it is a String object and it has the format that I want. But after that, I cannot create a Datetime object from a formatted string in 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 to get a Datetime object with the desired format, or do Datetime objects use this format, and you cannot format them in equivalent string formats?

thank

+3
3

, , . DateTime , ( DateTime.MinValue - ).

DateTime string , , DateTime.

+4

A DateTime - . "". . , , DateTime , , .

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

:

String date = currTime.ToString("s");
+9

, DateTime , (, string.Format()), # .ToString(). DateTime .ToString(), , .

DateTime , .ToString(IFormatProvider provider) .ToString(string format).

, , , , , DateTime , .ToString, :

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

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

Source: https://habr.com/ru/post/1759535/


All Articles