将 HH.mm 格式的字符串解析为 TimeSpan [英] Parse string in HH.mm format to TimeSpan

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

问题描述

我使用的是 .NET framework v 3.5,我需要将表示时间跨度的字符串解析为 TimeSpan 对象.

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 属性,它是一个 TimeSpan> 结构:

Parse out the DateTime and use its TimeOfDay property which is a TimeSpan structure:

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

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

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