Skip to content
Advertisement

In C# , how to set English time zone when formating a DateTIme?

private static SimpleDateFormat formatter = new SimpleDateFormat(“dd/MMM/yyyy:HH:mm:ss Z”, Locale.ENGLISH); string startTime = formatter.format(DateTime.Now); //请求时间

Those are Java codes. Can you help me convert the Java codes to equaivlent(equal) C#(.net) codes?

I know to use DateTime.Now.ToString(“dd/MMM/yyyy:HH:mm:ss Z “) ,but the core problem is that I don’t know how to set the time to English time. I have struggled for half a day, and it’s too difficult to me. who can give me a final result?

Advertisement

Answer

If you want to use a specific culture’s formatting with ToString(), use the DateTime.ToString(String, IFormatProvider) overload which allows you to pass a CultureInfo as the second argument.

Since you wanted “English time”, you could use this:

DateTime.Now.ToString("dd/MMM/yyyy:HH:mm:ss z", new CultureInfo("en-US"));

There’s also en-GB and many more. For more culture names, see the list here: https://msdn.microsoft.com/en-us/goglobal/bb896001.aspx . On Windows 10, you can also use any valid BCP-47 tag.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement