Home > Enterprise >  Date.ParseExact issues
Date.ParseExact issues

Time:10-12

I'm trying to format a date for an API. the desired format is: yyyy-MM-ddTHH:mm:ss.fffffff HH:mm (eg. 2022-10-12T09:52:14.1234567 03:00). I'm using Date.ParseExact in the following way:

Date.ParseExact("2022-10-12T09:52:14.1234567 03:00", "yyyy-MM-ddTHH:mm:ss.fffffff HH:mm", CultureInfo.InvariantCulture)

.

Initially I used 'Now' instead of this string, but then I saw that the string and the desired format have to match. The error I'm getting is 'DateTime pattern 'H' appears more than once with different values.'. Is there a way to avoid that? Also is it possible to use 'Now' in this line?

Thank you

CodePudding user response:

I suspect that you don't have a parse issue, you don't need ParseExact at all. You have a Date and want to return it as a formatted string. Then use ToString and zzz for the utc-offset:

string result = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz");

Read also: Custom date and time format strings

  • Related