本文介绍了时间跨度到 C# 中的本地化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的方法(可能是内置解决方案)将 TimeSpan
转换为本地化字符串?例如 new TimeSpan(3, 5, 0);
将转换为 3 hours, 5minutes
(仅在波兰语中).
Is there an easy way (maybe built in solution) to convert TimeSpan
to localized string? For example new TimeSpan(3, 5, 0);
would be converted to 3 hours, 5minutes
(just in polish language).
我当然可以创建自己的扩展程序:
I can of course create my own extension:
public static string ConvertToReadable(this TimeSpan timeSpan) {
int hours = timeSpan.Hours;
int minutes = timeSpan.Minutes;
int days = timeSpan.Days;
if (days > 0) {
return days + " dni " + hours + " godzin " + minutes + " minut";
} else {
return hours + " godzin " + minutes + " minut";
}
}
但是如果我想涉及正确的语法,这会变得复杂.
But this gets complicated if i want to have proper grammar involved.
推荐答案
我不认为这是可能的.你可以做的是这样的:
I do not think this is possible. What you can do is something like this:
public static string ConvertToReadable(this TimeSpan timeSpan) {
return string.Format("{0} {1} {2} {3} {4} {5}",
timeSpan.Days, (timeSpan.Days > 1 || timeSpan.Days == 0) ? "days" : "day",
timeSpan.Hours, (timeSpan.Hours > 1 || timeSpan.Hours == 0) ? "hours" : "hour",
timeSpan.Minutes, (timeSpan.Minutes > 1 || timeSpan.Minutes == 0) ? "minutes" : "minute");
}
这篇关于时间跨度到 C# 中的本地化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!