如何将字符串转换为TimeSpan [英] How to convert String to TimeSpan

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

问题描述

你好

我正在尝试将String转换为Time Span并分配给Time Control.

使用此代码:

Hello

I am trying to convert String into Time Span and assigning to Time Control.

Using this Code:

string CurrTime =lblPlaybackTime.Content.ToString();   //e.g. lblPlaBackTime.Content="14:02:11:"
                   startTimeCtrl.Value = TimeSpan.Parse(CurrTime.Remove(2, 1).Remove(4, 1).Remove(6, 1));




但不起作用.抛出异常.


谢谢




But Not working.throws exception.


Thanks

推荐答案

停止删除冒号:
Stop removing the colon characters:
TimeSpan ts = TimeSpan.Parse("12:15:06");

可以正常工作:12小时15分钟6秒.

[edit]错字:16秒而不是6秒-OriginalGriff [/edit]

Works fine: 12 hours, 15 minutes, and 6 seconds.

[edit]Typo: 16 seconds instead of 6 - OriginalGriff[/edit]


DateTime start_time = DateTime.Parse("1/9/2011");
DateTime spot_time = DateTime.Parse("5/9/2011");
TimeSpan diff = spot_time.Subtract(start_time);

希望对您有帮助....
DateTime start_time = DateTime.Parse("1/9/2011");
DateTime spot_time = DateTime.Parse("5/9/2011");
TimeSpan diff = spot_time.Subtract(start_time);

Hope this may help u ....


这篇关于如何将字符串转换为TimeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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