TimeSpan 到友好字符串库 (C#)
有谁知道一个好的库(或代码片段)用于将 TimeSpan 对象转换为“友好”字符串,例如:
- 两年三个月零四天
- 一周零两天
(它用于文档过期系统,其中过期时间可以是几天到几十年的任何时间)
只是为了澄清一下,假设我有一个 7 天的 TimeSpan,应该打印“1 周”、14 天“2 周”、366 天“1 年零 1 天”等ETC。
Does anyone know of a good library (or code snippet) for converting a TimeSpan object to a "friendly" string such as:
- Two years, three months and four days
- One week and two days
(It's for a document expiry system, where the expiry could be anything from a few days to several decades)
Just to clarify, say I had a TimeSpan with 7 days, that should print "1 week", 14 days "2 weeks", 366 days "1 year and 1 day", etc etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
请参阅how-do-i-calculate-relative-time,询问(< a href="https://stackoverflow.com/users/1/jeff-atwood">由 1 号用户)一年前,当时 SO 还很年轻,还没有公开。
它(可能)是 SO 问题和答案当前年龄显示的基础。
See how-do-i-calculate-relative-time, asked (by user number 1) a year ago when SO was young and not public.
It was (probably) the basis for the current age display for SO questions and answers.
是的! 我多次需要同样的东西,现在我创建了我的 onw 包并将其发布在 Nuget 中。 欢迎您使用它。
包名为EstecheAssemblies,
实现起来很简单:
Yes! I needed the same so many times then now I create my onw package and published it in Nuget. You are welcome to use it.
The package name is EstecheAssemblies
It's easy to implement:
要格式化任何超过 1 天的时间段(即月/年/十年等),Timespan 对象是不够的。
假设您的时间跨度为 35 天,那么从 4 月 1 日起,您将获得 1 个月零 5 天,而从 12 月 1 日起,您将获得 1 个月零 4 天。
To format any period longer than 1 day (i.e. month/year/decade etc.) a Timespan object is not enough.
Suppose your timespan is 35 days, then from Apr 1 you would get one month and five days, whereas from Dec 1 you would get one month and four days.
它可能不会完成您想要的所有操作,但在 v4 中,Microsoft 将 在
TimeSpan
上实现IFormattable
。It probably won't do everything you are looking for, but in v4 Microsoft will be implementing
IFormattable
onTimeSpan
.这是我的解决方案。 它基于此线程中的其他答案,并添加了对年份和月份的支持,正如原始问题中所要求的(也是我所需要的)。
至于讨论这是否有意义,我想说的是,确实有这样的情况。 就我而言,我们想要展示协议的持续时间,在某些情况下只有几天,而在其他情况下则长达几年。
测试;
执行;
Here is my solution to this. It is based on other answers in this thread, with added support for year and month as that was requested in the original question (and was what I needed).
As for the discussion whether or not this makes sense I would say that there are cases where it does so. In my case we wanted to show the duration of agreements that in some cases are just a few days, and in other cases several years.
Tests;
Implementation;
现在还有 Humanizer 项目,看起来非常有趣,可以做到这一点以及更多。
There is now also the Humanizer project that looks very interesting that can do this and way more.
TimeSpan 对象具有 Days、Hours、Minutes 和 Seconds 属性,因此制作将这些值格式化为友好字符串的代码片段不会太难。
不幸的是,天数是最大的值。 如果超过这个时间,您将不得不开始担心每年一个月中的几天......等等。 在我看来,你最好停下来几天(额外的努力似乎不值得)。
更新
...我想我应该从我自己的评论中提出这一点:
The TimeSpan object has Days, Hours, Minutes, and Seconds properties on it, so it wouldn't be too hard to make a snippet that formats those values to a friendly string.
Unfortunately Days is the largest value. Anything longer than that and you'll have to start worrying about days in a month for every year...etc. You're better off stopping at days in my opinion (the added effort doesn't seem worth the gain).
UPDATE
...I figured I'd bring this up from my own comment:
我知道这已经很旧了,但我想用一个很棒的 nuget 包来回答。
https://www.nuget.org/packages/Humanizer
https://github.com/MehdiK/Humanizer
他们的 readme.md 中的示例
@ian-becker 需要信用
I know this is old, but I wanted to answer with a great nuget package.
https://www.nuget.org/packages/Humanizer
https://github.com/MehdiK/Humanizer
Example from their readme.md
@ian-becker Needs the credit
不是一个功能齐全的实现,但它应该让您足够接近。
最后,您真的需要让别人知道文档将在 1 年、5 个月、2 周和 3 天后到期吗? 您不能告诉他们该文件将在 1 年后或 5 个月后过期吗? 只需取最大的单位并说出该单位的 n 即可。
Not a fully featured implementation, but it should get you close enough.
In the end, do you really need to let someone know a document is going to expire exactly 1 year, 5 months, 2 weeks, and 3 days from now? Can't you get by with telling them the document will expire over 1 year from now, or over 5 months from now? Just take the largest unit and say over n of that unit.
我只是偶然发现这个问题,因为我想做类似的事情。 经过一番谷歌搜索后,我仍然没有找到我想要的东西:以某种“圆形”方式显示时间跨度。 我的意思是:当某些事件持续了几天时,显示毫秒并不总是有意义的。 然而,当需要几分钟时,它可能会发生。 在这种情况下,我不想显示 0 天和 0 小时。 因此,我想参数化要显示的相关时间跨度部分的数量。 这导致了这段代码:
示例 (LinqPad):
显示:
适合我,看看它是否适合您。
I just stumbled upon this question because I wanted to do a similar thing. After some googling I still didn't find what I wanted: display a timespan in a sort of "rounded" fashion. I mean: when some event took several days, it doesn't always make sense to display the milliseconds. However, when it took minutes, it probably does. And in that case, I don't want 0 days and 0 hours to be displayed. So, I want to parametrize the number of relevant timespan parts to be displayed. This resulted in this bit of code:
Example (LinqPad):
Displays:
Suits me, see if it suits you.