I am using this expression to display the current date in textbox using rdlc report.
What is the expression for getting this output '22nd day of August 2018'?
Thanks in advance!
I don't believe there's a build-in method for getting a day's ordinal string ("st", "nd", "rd", and "th"), but you could easily write a method to do this, based on some well-known rules.
Here's one example which takes in an integer and returns the string value of that integer with the ordinal (like "1st", "2nd", "3rd", "4th"):
private static string GetNumberWithOrdinalIndicator(int number)
{
switch (number)
{
// Special cases for 11, 12, and 13
case 11: case 12: case 13:
return number + "th";
default:
switch (number % 10) // Last digit of number
{
case 1:
return number + "st";
case 2:
return number + "nd";
case 3:
return number + "rd";
default:
return number + "th";
}
}
}
Now with that method complete, we can create another method that constructs your custom date string using the parts of the date:
/// <summary>
/// Returns a custom date string, like "22nd day of August 2018"
/// </summary>
/// <param name="date">The date to use</param>
/// <returns>The custom formatted date string</returns>
private static string GetCustomDateString(DateTime date)
{
return $"{GetNumberWithOrdinalIndicator(date.Day)} day of {date.ToString("MMMM yyyy")}";
}
So finally, in use this might look like:
private static void Main()
{
var todaysCustomDateString = GetCustomDateString(DateTime.Today);
Console.WriteLine($"Today is the {todaysCustomDateString}");
GetKeyFromUser("\nDone! Press any key to exit...");
}
Which will output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.