C# 自定义日期格式
我希望在输出中显示“天”一词。我尝试将转义字符放在“-”前面、“days”中所有字符和空格前面。任何想法或指示。
这是我正在使用的行,totalTimeInRoom 是一个 TimeSpan:
totalTimeInRoom.ToString(@"dd\.hh\:mm\:ss");
编辑: 刚刚尝试过这个,没有运气:
totalTimeInRoom.ToString(@"dd \"Days(s)\" \- hh\:mm\:ss");
I would like to have the word "day(s)" displayed in the output. I've tried putting escape characters in front of just the "-", in front of all the characters and spaces in "days". Any ideas or pointers.
This is the line I'm working with, totalTimeInRoom is a TimeSpan:
totalTimeInRoom.ToString(@"dd\.hh\:mm\:ss");
EDIT:
Just tried this, no luck:
totalTimeInRoom.ToString(@"dd \"Days(s)\" \- hh\:mm\:ss");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如此处所述,您可以使用单引号字符嵌入文字字符串以您的自定义日期格式:
As explained here, you can use single quote characters to embed a literal string in your custom date format:
我之前一直在努力解决这个问题,并且总是最终使用 String.Format 进行“作弊”:
当然,您可以使其更高级,并且仅在时间跨度中实际有 1 天或更多天时才显示“天”,而不是无条件地显示使用“day(s)”,当天数大于 1 时,您可以动态附加 's'。
I've struggled with this before and always ended up "cheating" using String.Format:
Of course, you could make it much more advanced and only show 'days' if there's actually 1 or more days in the timespan and also instead of unconditionally using "day(s)" you can dynamically append an 's' when the number of days is more than 1.