在HH.mm格式时间跨度解析字符串跨度、字符串、格式、时间

2023-09-03 15:48:29 作者:夢醒丶淚已落

我用 .NET框架V 3.5 ,我需要分析一个字符串,再presenting时间跨度为时间跨度对象。

I'm using .NET framework v 3.5 and i need to parse a string representing a timespan into TimeSpan object.

问题是,点分离器是用来代替结肠...例如 13.00 22.30

The problem is that dot separator is used instead of colon... For example 13.00, or 22.30

所以,我想知道如果我要更换 或有一个更清洁的方式来获得此。

So I'm wondering if I have to replace . with : or there is a more clean way to obtain this.

推荐答案

解析出的DateTime 和使用它的的TimeOfDay 属性,它是一个时间跨度结构:

Parse out the DateTime and use it's TimeOfDay property which is a TimeSpan structure:

string s = "17.34";
var ts = DateTime.ParseExact(s, "HH.mm", CultureInfo.InvariantCulture).TimeOfDay;