C# WinForms DateTimePicker Custom Format
Asked Answered
L

1

1

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.

Laudanum answered 26/4, 2011 at 16:10 Comment(0)
V
4

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";
Vshaped answered 26/4, 2011 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.