Search code examples
c#datedate-formatting

Convert a string into Date


I have a string containing information 08/23/2016~08:00 - 12:00~D . I want to first convert it into date i.e 08/23/2016 and then convert this into Tuesday,August 23,2016 . Is it possible to convert it in this format?


Solution

  • A combination of string- and DateTime-methods

    string input = "08/23/2016~08:00 - 12:00~D";
    string datePart = input.Split('~')[0].Trim();
    DateTime dt;
    if (DateTime.TryParse(datePart, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt))
    {
        string output = dt.ToString("dddd,MMMM dd,yyyy", DateTimeFormatInfo.InvariantInfo);
    }
    

    Custom Date and Time Format Strings