Sitecore 在内容编辑器中以 24 小时格式显示时间

标签 sitecore sitecore7.2

在项目中,有一个字段类型“DateTime”。对于内容编辑器中的此字段,输入时间的选项采用 12 小时格式(上午、下午)。如下图:

enter image description here

如何向内容编辑器显示 24 小时格式的时间。

谢谢!

最佳答案

您可以通过扩展Sitecore.Shell.Applications.ContentEditor.DateTime来覆盖默认时间格式。类,找到 TimePicker在该控件内并更改 Format属性(默认设置为 t,这意味着将使用内容编辑器用户的标准区域设置)。老实说,我不认为任何 Sitecore 解决方案都应该建议更改此设置,但如果需要,这是可行的。

首先,创建继承自 Sitecore.Shell.Applications.ContentEditor.DateTime 的自定义类,例如:

namespace My.Assembly.Namespace.ContentEditor
{
    public class CustomDateTime : Sitecore.Shell.Applications.ContentEditor.DateTime
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            TimePicker timePicker = FindTimePicker(this);
            if (timePicker != null)
            {
                timePicker.Format = "HH:mm";
            }
        }

        private TimePicker FindTimePicker(Control control)
        {
            if (control != null)
            {
                var timePicker = control as TimePicker;

                if (timePicker != null)
                {
                    return timePicker;
                }

                foreach (Control childControl in control.Controls)
                {
                    if ((timePicker = FindTimePicker(childControl)) != null)
                    {
                        return timePicker;
                    }
                }
            }
            return null;
        }
    }
}

现在在web.config查找<controlSources>标签,并在该标签内添加以下行:

  <source mode="on" namespace="My.Assembly.Namespace.ContentEditor" assembly="My.Assembly" prefix="customcontent"/>

现在转到Sitecore Desktop,切换到core数据库,找到/sitecore/system/Field types/Simple Types/Datetime Control 的项目和更改值字段至 customcontent:CustomDateTime :

sitecore - date time picker - custom format

切换回数据库并检查任何日期时间字段,例如:

sitecore - date time picker - custom format

关于Sitecore 在内容编辑器中以 24 小时格式显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32840706/

相关文章:

sitecore - 使用 Sitecore 发布代理并在特定时间发布项目

Sitecore BucketItem 文件夹版本控制问题

indexing - Sitecore solr索引重建和刷新之间的区别

sitecore - 当入口页面是别名时,我应该如何启动 Sitecore 营销事件?

xml - 使用 XPath/Sitecore 查询获取层次结构中特定级别的祖先

logging - 使用 SIM-Tool 导入后 Sitecore 实例不再记录

c# - TreeListEx,使用模板 _StandardValues 设置的字段未从 sitecore 检索?

sitecore - 我们可以在 session 中存储 Sitecore 项目吗

asp.net-mvc - @Html.Sitecore.Placeholder() 指向什么?

c# - Sitecore 如何通过管道以英语添加项目的版本