I came across some notation in an interpolated string currentDate:d
in the Microsoft documentation but they do not elaborate on how it works or what it is called, so I don't know how to look it up further. It appears to deconstruct a DateTime and get the date and time specifically, and only seems to work in the interpolated string; I can't use the same trick to pull the time out into a variable. I'm wondering if I can use that notation for other objects and how it works.
var currentDate = DateTime.Now;
var time = currentDate:t; // this throws error
Console.WriteLine($"{Environment.NewLine}It is currently {currentDate:d} at {currentDate:t}!");
outputs:
It is currently 2021-10-31 at 10:41 AM!
MS docs source: https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code?pivots=dotnet-5-0
I can't think of a better way to ask this question, so would appreciate a hint.
CodePudding user response:
It is explained in the docs that you linked. It says:
The dollar sign (
$
) in front of a string lets you put expressions such as variable names in curly braces in the string. The expression value is inserted into the string in place of the expression. This syntax is referred to as interpolated strings.
The link there takes you to the page "$ - string interpolation - C# reference", which has a section named "Structure of an interpolated string" that says this:
The structure of an item with an interpolation expression is as follows:
{<interpolationExpression>[,<alignment>][:<formatString>]}
[...]
formatString
: A format string that is supported by the type of the expression result. For more information, see Format String Component.
The link "Format String Component" then gives you all the info you need:
The optional
formatString
component is a format string that is appropriate for the type of object being formatted. Specify a standard or custom numeric format string if the corresponding object is a numeric value, a standard or custom date and time format string if the corresponding object is a DateTime object, or an enumeration format string if the corresponding object is an enumeration value.[...]
Date and time types: Standard Date and Time Format Strings / Custom Date and Time Format Strings
The linked page "Standard Date and Time Format Strings" explains d
as follows:
Format Specifier Description Examples d Short date pattern. - More information: The short date ("d") format specifier 2009-06-15T13:45:30 -> 6/15/2009 (en-US) [...]
This already explains it in short, but the link provided in the table leads to an even more detailed explanation.
This also shows an example of how to use such a format string outside of an interpolated string, using the ToString
method:
DateTime date1 = new DateTime(2008, 4, 10); Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo)); // Displays 04/10/2008 Console.WriteLine(date1.ToString("d", CultureInfo.CreateSpecificCulture("en-US"))); // Displays 4/10/2008 Console.WriteLine(date1.ToString("d", CultureInfo.CreateSpecificCulture("en-NZ"))); // Displays 10/04/2008 Console.WriteLine(date1.ToString("d", CultureInfo.CreateSpecificCulture("de-DE"))); // Displays 10.04.2008
CodePudding user response:
It's just some of the standard date and time format specifiers.
"d"
is the short date format specifier, "t"
is the short time format specifier.
More info can be found at Standard date and time format strings on MSDocs.
Aside: You could have achieved this without string interpolation as well:
Console.WriteLine("It is currently {0:d} at {0:t}!", currentDate);