2

I have a CheckBox that I want to say;

"Load Prior to: 01/01/2001"

But instead the content says;

"Load Prior to: 01 01 2001"

Basically there are no slashes. This is my how I set the content;

oldContactsCheckBox.Content = 
  "Load Contracts Prior To: " + 
   DateTime.Today.AddYears(-3).ToString("dd/MM/yyyy");

How can I change this so that the slashes are included in the formatting?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
CBreeze
  • 2,925
  • 4
  • 38
  • 93
  • 1
    This [Questions](http://stackoverflow.com/questions/6362088/c-sharp-date-formatting-is-losing-slash-separators) is your answer. – moien Oct 10 '16 at 14:08

1 Answers1

4

Try escaping:

https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx#escape

oldContactsCheckBox.Content = 
  "Load Contracts Prior To: " + 
  DateTime.Today.AddYears(-3).ToString(@"dd\/MM\/yyyy");

A better implementation is string interpolation (C# 6.0):

oldContactsCheckBox.Content = 
  $@"Load Contracts Prior To: {DateTime.Today.AddYears(-3):dd\/MM\/yyyy}";

or formatting:

oldContactsCheckBox.Content = string.Format(
  @"Load Contracts Prior To: {0:dd\/MM\/yyyy}",
    DateTime.Today.AddYears(-3));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215