Steve Chapman
Steve Chapman

Reputation: 1317

Showing Datetime using C#

I need to show timestamp as shown below in my .Net application:

13/12/2007 5:04 PM EST

or

13/12/2007 5:04 PM CST

depending upon the US timezone.

How do i achieve this functionality using C#??

Thanks for reading.

Upvotes: 1

Views: 955

Answers (4)

Mohamed hachem
Mohamed hachem

Reputation:

Hello Here is your solotion

    private string ShortTimeZone(string timeZoneFormat)
    {
        string[] TimeZoneElements = timeZoneFormat.Split(' ');
        string shortTimeZone = String.Empty;

        foreach (string element in TimeZoneElements)
        {
            shortTimeZone += element[0];
        }

        return shortTimeZone;
    }

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

You can just call a DateTime's .ToLongDateString() method and it will format the result according to the settings on the local system.

Upvotes: 4

Inisheer
Inisheer

Reputation: 20794

Console.WriteLine(DateTime.Now + " " + TimeZone.CurrentTimeZone.StandardName);

returns

6/10/2009 7:45:14 PM Central Standard Time

TimeZone.CurrentTimeZone.StandardName will return the long name and I believe you will have to modify your code a bit to get the abr. for each zone.

Upvotes: 5

Lee
Lee

Reputation: 18747

Use the DateTime.ToString(string) method:

DateTime.ToString("dd/MM/yyyy h:mm t K")

This will not match your output format exactly, but it will come close. Since "EST" and "CST" aren't international-friendly, it just displays a divergence from UTC time.

Upvotes: 1

Related Questions