C#Library解析人类可读的时间跨度 [英] C# Library to parse human readable time spans
问题描述
有没有一个库可以将人类可读的时间片解析为.net TimeSpan?
Is there a library that exists that will parse human readable timespans into a .net TimeSpan?
我需要一些解析字符串的东西,如
I need something that will parse strings like
30 days
1 week
5 hours
这样的事情是否存在?它可能不是太难写自己,但如果有东西在那里,这将会更容易!
Does such a thing exist? Its probably not too hard to write myself, but if something is out there, it would be so much easier!
我目前不需要像30天, 3小时5分钟,只是一个值,但不能伤害!
I currently don't need functionality like "30 days, 3 hours, 5 minutes", just the one value, but it couldn't hurt!
推荐答案
有时候,我正在玩解析器,因为我对它们一无所知,所以不要期待质量。但您可以在此处获取代码: http://timestamper.codeplex.com/
I took a crack at this sometime back, I was playing with parsers as I knew nothing about them so don't expect quality. But you can get the code here: http://timestamper.codeplex.com/
我没有看过它一段时间,但我相当确定它可以做你上面提到的所有事情。
I havent looked at it for a while, but im pretty sure it can do all the things that you mentioned above.
编辑:
刚刚注意到你想要一个timepan对象返回而不是一个日期时间。我提供的代码将返回到今天的DateTime reletive,例如
Just noted that you wanted a timespan object returned and not a date time. The code I provided will return a DateTime reletive to today i.e.
2天将是:DateTime.Now.AddDays(2);
"2 days" will be: DateTime.Now.AddDays(2);
至少我认为这是做的,可以肯定的。如果是这种情况,您应该能够获得两个DateTimes之间的时间间隔。
At least I think this is what is did, can quite be sure. If this is the case, you should just be able to get the timespan between the two DateTimes.
这篇关于C#Library解析人类可读的时间跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!