在HH.mm格式时间跨度解析字符串 [英] Parse string in HH.mm format to TimeSpan

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

问题描述

我用 .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;

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

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