C# - how to convert string to date

  • Hi everyone

    I am not too experienced using C#.  I need to convert date in string format to date format.  I do not want any time attached to the date.

    The files have naming convention "fileIdentifer_YYYY-MM-DD".  I am able to extract "YYYY-MM-DD":

    string fileDate = "";

    fileDate = fileName.Substring(fileName.Length - 14, 10);

    Now, I need to convert fileDate so it is a date type.

    How can I do this?

    Thank you

  • This should do it:

    string fileDate = "2024-03-31";

    DateTime someDate = DateTime.ParseExact(fileDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);

    • This reply was modified 8 months, 1 week ago by  Phil Parkin.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • Thank you so much!

  • This was removed by the editor as SPAM

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply