Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use format string from a property

Tags:

c#

c#-6.0

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?

like image 732
mishan Avatar asked Mar 23 '16 10:03

mishan


1 Answers

Try this:

string format = "dd.MM.yyyy";
Console.WriteLine($"{DateOfSmth.ToString(format)}");
like image 154
diiN__________ Avatar answered Sep 29 '22 02:09

diiN__________