Home > OS >  Seting DateTimeFormatInto to show date in format "yyyy-MM-ddTHH:mm:ss", C#
Seting DateTimeFormatInto to show date in format "yyyy-MM-ddTHH:mm:ss", C#

Time:11-01

I am trying to set DateTime format to correspond "yyyy-MM-ddTHH:mm:ss". I managed to set format to "yyyy-MM-dd HH:mm:ss", but "T" between Date and Time is mandatory for call to SOAP service.

I don't need string in that format, but DateTime that has that value.

I tried with

int lcid = CultureInfo.CurrentCulture.LCID;
var formatInfo = new CultureInfo(lcid).DateTimeFormat;
            
formatInfo.DateSeparator = "-";
formatInfo.ShortDatePattern = "yyyy-MM-dd";
formatInfo.LongTimePattern = "HH:mm:ss";
formatInfo.FullDateTimePattern = "yyyy-MM-dd'T'HH:mm:ss";
            
Thread.CurrentThread.CurrentCulture = new CultureInfo(lcid, true);
Thread.CurrentThread.CurrentCulture.DateTimeFormat = formatInfo;

string sd = "2022-10-31T13:00:00";

DateTime sdConverted = DateTime.ParseExact(sd, "yyyy-MM-ddTHH:mm:ss", Thread.CurrentThread.CurrentCulture);

result is DateTime in format "2022-10-01 13:00:00".

EDIT: SOAP request creation

async Task<GetTimeResponse> GetTimeAsync(DateTime startDate, DateTime endDate, string username, string password)
{
   ServiceClient client = new ServiceClient();
   client.ClientCredentials.UserName.UserName = username;
   client.ClientCredentials.UserName.Password = password;

   TimeRequest timeRequest = new TimeRequest
   {
      From = startDate,
      Until = endDate,                    
   };

   GetTimeRequest request = new GetTimeRequest(timeRequest);
   GetTimeResponse response = await client.GetTimeAsync(request);
   return response;
}

Thank you :)

CodePudding user response:

Like said here the date time format you want is a standard type. It has format specifier "s".

E.g.

Console.WriteLine(DateTime.Now.ToString("s"));

Will print something like

2022-10-31T21:33:06

CodePudding user response:

Thank you so much for your answers.

It lead me to look the other way.

Solution was to change Reference.cs file of the SOAP service, and in object i am sending change DateTime to string.

As @JonSkeet stated in the comment

  • Related