12

the situation I'm in is following:

I have an interpolated string looking like this:

DateTime DateOfSmth;
string PlaceOfSmth;
$"{DateOfSmth}, {PlaceOfSmth}".Trim(' ',',');

And a format that should be used on it in:

string Format = "{0:dd.MM.yyyy}";

Now I'd like to use the format in the property Format in the interpolated string, but I don't know how.

I.E: i'd like something like this to be the result:

$"{DateOfSmth:Format}, {PlaceOfSmth}".Trim(' ',',');

Could someone help?

2
  • 1
    you can do like this Console.WriteLine($"Name = {name}, hours = {hours:hh}") for more detail please visit msdn.microsoft.com/en-us/library/dn961160.aspx
    – rashfmnb
    Commented Mar 23, 2016 at 10:29
  • Thanks for the link. I've already studied it before i posted the question. The real reason i need it like i said is because i need to use the formatting string from the Format property. and not write it directly into the string.
    – mishan
    Commented Mar 23, 2016 at 10:33

2 Answers 2

14

Try this:

string format = "dd.MM.yyyy";
Console.WriteLine($"{DateOfSmth.ToString(format)}");
2
  • In the end i used something a little bit similar. Because the format string has content {0:dd.MM.yyyy} I could not use your answer directly, but this helped a lot. thanks :)
    – mishan
    Commented Mar 23, 2016 at 11:01
  • just to cflarify - the format string is given and i cannot change it.
    – mishan
    Commented Mar 23, 2016 at 11:02
0

In the end I left the accepted answer to @diiN_. The solution i used is:

//this format is given and I can't change it
string Format = "{0:dd.MM.yyyy}";
DateTime? Date = DateTime.Today;
string Place = "Washington";

string.Format(Format + ", {1}", Date, Place);

Not the answer you're looking for? Browse other questions tagged or ask your own question.