C# WinForms DateTimePicker Custom Format

被刻印的时光 ゝ 提交于 2019-12-11 08:43:12

问题


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.

For example, given the following format:

dateTimePicker.CustomFormat = "dd MMM yyyy (YTT) HH:mm";

To produce the following display at runtime on 16th April 2011 at 12:00:

"16 Apr 2011 (today) 12:00"

I expect this is something involving building a IFormatProvider/ICustomFormatter implementation and hacking customising the CurrentCulture to use it.

Many thanks in advance.


回答1:


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";


来源:https://stackoverflow.com/questions/5793163/c-sharp-winforms-datetimepicker-custom-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!