.net文化特定的12/24小时格式 [英] .net Culture specific 12/24 hour formatting

查看:49
本文介绍了.net文化特定的12/24小时格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以保留区域性特定的日期时间格式,但可以强制进行12/24小时的渲染?我知道我可以对实际的日期/时间格式字符串做很多事情,例如 HH:mm:ss hh:mm:ss 文化格式(例如 mm/dd/yyyy yyyy/mm/dd 等),只需强制执行12/24小时制即可.

Is there a way to keep the culture specific date time formatting but force 12/24 hour rendering? I know I can do a lot with the actual date/time format string like HH:mm:ss and hh:mm:ss but I would like to honor the current user culture formatting (i.e. mm/dd/yyyy or yyyy/mm/dd, etc), just force 12/24 hour time rendering.

推荐答案

我可能会做这样的事情:

I'd probably do something like this:

        var culture = CultureInfo.CurrentCulture;
        var pattern = culture.DateTimeFormat.LongTimePattern; // or pick which one you want to use;
        var newPattern = pattern.Replace("h", "H").Replace("t", "");
        DateTime.Now.ToString(newPattern); // or use whatever DateTime you want to use

通过聊天:

以下是所有文化的长期模式字符串以及如何对其进行修改的列表:

Here is a list of all cultures' long time pattern strings, and how they would be modified:

Old: hh:mm:ss tt New: HH:mm:ss 
Old: HH:mm:ss 'ч.' New: HH:mm:ss 'ч.'
Old: HH:mm:ss New: HH:mm:ss
Old: H:mm:ss New: H:mm:ss
Old: h:mm:ss tt New: H:mm:ss 
Old: tt h:mm:ss New:  H:mm:ss
Old: h:mm:ss.tt New: H:mm:ss.
Old: HH.mm.ss New: HH.mm.ss
Old: tt hh:mm:ss New:  HH:mm:ss

这篇关于.net文化特定的12/24小时格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆