is there a way to ignore part of a datetime string when using DateTime.TryParseExact?

StackOverflow https://stackoverflow.com/questions/23689064

  •  23-07-2023
  •  | 
  •  

Pregunta

My datetime string looks like this

19430403000000-0400

But I only care about the values before the dash (i know its UTC offset, but I don't require that offset value)

So I want to use DateTime.TryParseExact if possible since I have a class that already implements it where the user passes the datetime format as string

So for this I want something like

"yyyyMMddhhmmss-####"

where the user can ignore parts of the string format

Is that possible?

¿Fue útil?

Solución

I think I still have to specify the entire format, I just have to use DateTimeOffset instead of DateTime. Seems to parse it alright.

string date8 = "19430403000000-0400";

        DateTimeOffset result2;
        bool parsed = DateTimeOffset.TryParseExact(date8, "yyyyMMddhhmmss zzzz", CultureInfo.InvariantCulture,
                               DateTimeStyles.AllowWhiteSpaces,
                               out result2);

Otros consejos

string str = "19430403000000-0400";
string output = str.Split("-")[0];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top