C# Datetime format. Localized strings like "sec" "year"
I need to show for a customer formated datetime like "12 sec"开发者_如何转开发 "2011 year"
How can i get localized string "sec", "year". Is in .net exists special format to get those strings?No, there isn't.
You will need to build resource files that contain these localized values and use them.
No, but you can use something like this.
DateTime.ToString("ss") + "sec";
Best you can do is:
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);
}
You can also create an enum for this and pass it to your method and return your string depending on the enum.
Using the method DateTime.ToString(String, IformatProvider), you can customize the DateTime's representation a lot (see the method documentation for more info). Some options are already localized using this API, but you would need a custom resource file for localized versions of "sec", year", etc.
精彩评论