How to display DateTime with an abbreviated Time Zone?

前端 未结 8 985
长情又很酷
长情又很酷 2020-12-08 19:02

I am aware of the System.TimeZone class as well as the many uses of the DateTime.ToString() method. What I haven\'t been able to find is a way to convert a DateTime to a st

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 19:40

    Use nodatime.

    The following function takes a DateTime in UTC time and formats it with abbreviated local system timezone. Use x in format string for abbreviated timezone. Look for custom formatting here.

        public static string ConvertToFormattedLocalTimeWithTimezone(DateTime dateTimeUtc)
        {
            var tz = DateTimeZoneProviders.Tzdb.GetSystemDefault(); // Get the system's time zone
            var zdt = new ZonedDateTime(Instant.FromDateTimeUtc(dateTimeUtc), tz);
            return zdt.ToString("MM'/'dd'/'yyyy' 'hh':'mm':'ss' 'tt' 'x", CultureInfo.InvariantCulture);
        }
    

提交回复
热议问题