C # Datetime format. Localized strings like "sec" "year"

I need to show for the client formed by datetime, as "12 seconds", "2011"
How can I get the localized string "sec", "year". Is there a special format in .net for receiving these strings?

+3
source share
4 answers

No, but you can use something like this.

DateTime.ToString("ss") + "sec";
+3
source

No no.

You will need to create resource files containing these localized values ​​and use them.

+5
source

, , :

private const string yearText = "{0} year"; // do this for month or hour or minute..
DateTime date = new DateTime(1999, 12, 10, 5, 10, 16);
string year = GetYearString(date);

private string GetYearString(DateTime date)
{
      return string.Format(yearText, date.Year);
}

.

+2

Using the DateTime.ToString (String, IformatProvider) method, you can customize the DateTime view a lot (see the method documentation for more information). Some parameters are already localized using this API, but you will need a special resource file for localized versions of "sec", year ", etc.

+2
source

Source: https://habr.com/ru/post/1784528/


All Articles