Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert DateTime "Thu Nov 30 19:00:00 EST 2006" to "11/30/2006"

Tags:

c#

datetime

How can I convert DateTime "Thu Nov 30 19:00:00 EST 2006" to "11/30/2006"

like image 303
afin Avatar asked Dec 17 '22 01:12

afin


1 Answers

Try something like this:

using System;
using System.Globalization;

class Example
{
    static void Main()
    {
        DateTime dateTime = DateTime.ParseExact("Thu Nov 30 19:00:00 EST 2006", 
            "ddd MMM dd HH:mm:ss EST yyyy", 
            CultureInfo.InvariantCulture);
        Console.WriteLine(dateTime.ToString("MM/dd/yyyy"));
    }
}

The .NET framework does not support time zone abbreviations so I hard-coded "EST" into the format string (just something to be aware of if you will need to parse strings from multiple time zones).

like image 103
Andrew Hare Avatar answered Jan 11 '23 23:01

Andrew Hare