转自:http://blog.163.com/y_p_xu/blog/static/17085710220116472030543/

/// <summary>
        /// 将时间“00:00:00”转化为单位为秒的数值
        /// </summary>
        /// <param name="time">形式为"00:00:00"的时间字符串</param>
        /// <returns>秒数</returns>
        public static int ConvertTimeToSecond(string time)
        {
            string[] strSplits = time.Split(':');
            if (strSplits.Length != 3)
                return -1;//非法时间格式

try
            {
                TimeSpan span = new TimeSpan(int.Parse(strSplits[0]), int.Parse(strSplits[1]), int.Parse(strSplits[2]));
                return (int)(span.Ticks / (1000 * 10000));
            }
            catch
            {
                return -1;
            }

}

/// <summary>
        /// 将单位为秒的数值转化为“00:00:00”时间格式
        /// </summary>
        /// <param name="seconds">秒数</param>
        /// <returns>形式为"00:00:00"的时间字符串</returns>
        public static string ConvertSecondToTime(int seconds)
        {
            int h = seconds / 3600;
            int m = (seconds - 3600 * h) / 60;
            int s = (seconds - 3600 * h) % 60;

string len = h.ToString().PadLeft(2, '0') + ":" + m.ToString().PadLeft(2, '0') + ":" + s.ToString().PadLeft(2, '0');

return len;
        }

c# TimeSpan的更多相关文章

  1. 从配置读取一段时间(TimeSpan)

    C#的TimeSpan表示一段时间,DateTime表示一个时间点.TimeSpan可正可负,可与DateTime相加减,很方便,我喜欢. 代码中我们经常要表示一段时间,用一个统一的单位(时 或者 分 ...

  2. C# TimeSpan 计算时间差(时间间隔)

    命名空间:System 程序集:mscorlib(在 mscorlib.dll 中) 说明: 1.DateTime值类型代表了一个从公元0001年1月1日0点0分0秒到公元9999年12月31日23点 ...

  3. C# 计算时间差 用timespan函数

    TimeSpan 结构  表示一个时间间隔. 命名空间:System 程序集:mscorlib(在 mscorlib.dll 中) 1.DateTime值类型代表了一个从公元0001年1月1日0点0分 ...

  4. Javascript倒计时组件new TimeSpan(hours, minutes, minutes)

    function TimeSpan(h, m, s) { this.h = Number(h); this.m = Number(m); this.s = Number(s); } TimeSpan. ...

  5. 转:TimeSpan的用法

    转:http://www.cnblogs.com/shuang121/archive/2011/03/03/1969583.html 举例:时间增加一天:DateTime.Parse(txt_Date ...

  6. 找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)"的解决方案

    找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)" http://www.microsoft.com/down ...

  7. C#时间处理--DateTime和TimeSpan

    DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.ToFileTime().ToString();//12775641 ...

  8. [C.Sharp] TimeSpan的用法,获取测试程序运行时间

    TimeSpan的用法 TimeSpan是用来表示一个时间段的实例,两个时间的差可以构成一个TimeSpan实例,现在就来简单介绍一下几点重要的用法: a 先来介绍几个方法 TimeSpan.Minu ...

  9. C# 使用TimeSpan计算两个时间差

    转载:http://www.cnblogs.com/wifi/articles/2439916.html 可以加两个日期之间任何一个时间单位. private string DateDiff(Date ...

随机推荐

  1. git pull request

    如何发 PR 以下以 wiki-pages 为例 把项目 fork 到自己名下,然后 clone 到本地 git clone git@code.xiaojukeji.com:yexiliang/wik ...

  2. testng 教程之使用参数的一些tricks配合使用reportng

    前两次的总结:testng annotation生命周期 http://www.cnblogs.com/tobecrazy/p/4579414.html testng.xml的使用和基本配置http: ...

  3. 向Maven的本地库中添加jar文件

    有时我们要用的 maven 依赖项在官方repo库中找不到,然而我们从其他渠道获得了依赖项中的所有jar文件,本文记录了如何向本地库添加jar文件. 从复杂到简单,有三种方法: 使用 maven 的仓 ...

  4. webstorm抽取函数

    webstrom 1.extact 抽取函数:选中代码,右键,refactor-extact function matchPicLink() { var $match = $('#match'); v ...

  5. CSS中定义CLASS时,中间有空格和每空格的区别

    css选择器的格式,规定不带空格的选择条件之间是“且”关系,带空格的是“父子”关系,并且可以是非直接的“父子”关系 <style> .e1.e2 { background-color:ye ...

  6. Win10 Migrate apps to the Universal Windows Platform (UWP)

    https://msdn.microsoft.com/en-us/library/mt148501.aspx

  7. TreeView checkbox 全选

    在使用TreeView 控件 ,进行权限管理的时候,需要使用 checkbox全选. 勾选父节点,子节点全部选中.取消父节点,子节点不选中. 勾选子节点,父节点也选中. 以下是在使用的例子: < ...

  8. [perl]字符串转拼音首字母(支持多音字)

    实现的思路是,查表找到该字的所有读音,然后取首字母. 代码: while (<DATA>) { chomp; })(.*)$/; $all =~ s/^\s+//; ### 只保留无音标号 ...

  9. 解决VS+opencv中Debug版本与Release版本lib切换的问题

    Author: Maddock Date: 2015-03-26 09:34:48 问题来源:http://bbs.csdn.net/topics/390733725 PS: 按照上述方法做的时候,在 ...

  10. npm-bluebird使用

    API 注意 时刻注意return; 使用Promise.promisify简化对test(val, function(err, result){})的处理; 尽量避免使用deferred objec ...