Home > Enterprise >  Need to convert simpledateformat(EEE MMM d HH:mm:ss z yyyy) to DateTime in C#
Need to convert simpledateformat(EEE MMM d HH:mm:ss z yyyy) to DateTime in C#

Time:09-22

I am receiving the API responses and doing some logic.

I am trying to convert a string to DateTime but I am unable to convert it in C#. I tried multiple ways but I am getting string in invalid format.

I need to convert "EEE MMM d HH:mm:ss z yyyy" to an equivalent DateTime format in C#.

I have two dates as below and need to calculate the total days from the two dates

var dt1="Wed Jul 14 07:59:30 BST 2021"; var dt2="Fri Jul 16 08:59:30 BST 2021";

Please give me any suggestions.

CodePudding user response:

Here is what you need:

using System;
using System.Globalization;
public class Program
{
    public static void Main()
    {
        String yourDate = "Wed Jul 14 07:59:30 BST 2021";
        DateTime dt = DateTime.ParseExact( yourDate,"ddd MMM dd HH:mm:ss 'BST' yyyy", CultureInfo.InvariantCulture);
        Console.WriteLine(dt.ToString());
    }
}

It will give you this output: 7/14/2021 7:59:30 AM

  • Related