C#WinForms DateTimePicker自定义格式 [英] C# WinForms DateTimePicker Custom Format
问题描述
我的表单上有一个 DateTimePicker
。我希望格式的一部分适当地包含今天,明天等。我有一个简单的实现来计算值(这里还有一个更好的方法:此处),但我想采用该实现并将其值直接放入 DateTimePicker $ c $的
CustomFormat
属性中c>。
I have a DateTimePicker
on my form. I'd like part of the format to include "today", "tomorrow" etc as appropriate. I've got a simple implementation to work out the value (and there are much better ones here) but I'd like to take that implementation and put the value directly into the CustomFormat
property of the DateTimePicker
.
例如,给定以下格式:
dateTimePicker.CustomFormat = "dd MMM yyyy (YTT) HH:mm";
要在2011年4月16日12:00的运行时显示以下内容:
To produce the following display at runtime on 16th April 2011 at 12:00:
"16 Apr 2011 (today) 12:00"
我希望这涉及到构建 IFormatProvider
/ ICustomFormatter
的实现和 hacking 定制 CurrentCulture
来使用它。
I expect this is something involving building a IFormatProvider
/ICustomFormatter
implementation and hacking customising the CurrentCulture
to use it.
非常感谢。
推荐答案
在自定义日期格式字符串中的静态文本周围使用'
。
Use '
around the static text in a custom date format string.
string ytt = "today"; //Simple example... Update to determine yesterday, today or tomorrow.
this.dateTimePicker.CustomFormat = "dd MMM yyyy '(" + ytt + ")' HH:mm";
这篇关于C#WinForms DateTimePicker自定义格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!