4

I have a date/time string that looks like the following:

Wed Sep 21 2011 12:35 PM Pacific

How do I format a DateTime to look like this?

Thank you!

2
  • 1
    I use this guy's cheat sheet all the time: blog.stevex.net/string-formatting-in-csharp Commented Sep 7, 2011 at 21:20
  • 3
    Is it always Pacific? Because I don't believe the .NET DateTime formatting will give you the time zone name. Commented Sep 7, 2011 at 21:20

4 Answers 4

9

The bit before the time zone is easy, using a custom date and time format string:

string text = date.ToString("ddd MMM dd yyyy hh:mm t");

However, I believe that the .NET date/time formatting will not give you the "Pacific" part. The best it can give you is the time zone offset from UTC. That's fine if you can get the time zone name in some other way.

A number of TimeZoneInfo identifiers include the word Pacific, but there isn't one which is just "Pacific".

Sign up to request clarification or add additional context in comments.

4 Comments

Isn't it kind of redundant to give 24 hour time and the AM/PM? :P
+1, and an offset of -8 may not always mean Pacific - many named zones share the same offset, and DateTime/DateTimeOffset don't store it.
@Brandon: Yes, doh. Of course it's possible that it still is 24 hour - we can't tell. (There's already redundancy in the format with the name of the day of the week of course.)
@Paul: You're preaching to the choir :) Having said that, even Noda Time wouldn't have just "Pacific" as a time zone ID...
6
string.Format("{0} {1}", DateTime.Now.ToString("ddd MMM dd yyyy HH:mm tt"), TimeZone.CurrentTimeZone.StandardName);
//Result: Wed Sep 07 2011 14:29 PM Pacific Standard Time

Trim off the Standard Time if you do not want that showing.

EDIT: If you need to do this all over the place you can also extend DateTime to include a method to do this for you.

void Main()
{
    Console.WriteLine(DateTime.Now.MyCustomToString());
}

// Define other methods and classes here
public static class DateTimeExtensions
{
    public static string MyCustomToString(this DateTime dt)
    {
        return string.Format("{0} {1}", DateTime.Now.ToString("ddd MMM dd yyyy HH:mm tt"), TimeZone.CurrentTimeZone.StandardName).Replace(" Standard Time", string.Empty);
    }
}

You can run this example in LinqPad with a straight copy and paste and run it in Program mode.

MORE EDIT

After comments from below this is the updated version.

void Main()
{
    Console.WriteLine(DateTime.Now.MyCustomToString());
}

// Define other methods and classes here
public static class DateTimeExtensions
{
    public static string MyCustomToString(this DateTime dt)
    {
        return string.Format("{0:ddd MMM dd yyyy hh:mm tt} {1}", DateTime.Now, TimeZone.CurrentTimeZone.StandardName).Replace(" Standard Time", string.Empty);
    }
}

2 Comments

There's no need to mix String.Format and ToString at the same time. You can do it all in one call: String.Format("{0:ddd MMM dd yyyy HH:mm tt} {1}", DateTime.Now, TimeZone.CurrentTimeZone.StandardName)
Oh, whoops. The HH should be hh for 12-hour time.
5

Take a look at the docs about Custom Date and Time Format Strings.

Comments

2

Note this may be a bit crude, but it may lead you in the right direction.

Taking and adding to what Jon mentioned:

string text = date.ToString("ddd MMM dd yyyy hh:mm t");

And then adding something along these lines:

    TimeZone localZone = TimeZone.CurrentTimeZone;
    string x = localZone.StandardName.ToString();
    string split = x.Substring(0,7);
    string text = date.ToString("ddd MMM dd yyyy hh:mm t") + " " + split;

I haven't tested it, but i hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.