从包含格式的字符串中解析TimeSpan [英] Parsing TimeSpan from string including format

查看:44
本文介绍了从包含格式的字符串中解析TimeSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定这很简单,但是我不知道如何在Google中正确地写出文字...

I'm sure this must be simple, but I can't figure out how to word it correctly in Google...

我有一个配置,其中有一个字段:

I have a config which has a field:

TimeToPoll="1d"

现在我想做类似的事情:

TimeSpan.Parse(TimeToPoll);

返回一天的时间段.

在C#中

我正在寻找一种允许解析"1d"以及"1s"或"1y"等的方法.这可能吗?

I'm looking for a method which allows parse of "1d" as well as "1s" or "1y" etc. Is this possible?

含义:

     "1d" parses to {1.00:00:00}
     "1h" parses to {0.01:00:00}
     "1m" parses to {0.00:01:00}
     "1s" parses to {0.00:00:01}

推荐答案

不需要 d ,这是解析失败的原因.

The d is not needed and is the reason your parse fails.

var oneDay = TimeSpan.Parse("1");


更新:

对于您想要做的事情,没有内置的支持.您将需要编写自己的解析器.

For what you are looking to do, there is no built in support. You would need to write your own parser.

这篇关于从包含格式的字符串中解析TimeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆