转自: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. linux常用命令积累

    1.jps jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/u ...

  2. ABAP 合并单元格自建函数

    FORM frm_merge_cells USING sor_cell tar_cell.   CALL METHOD OF excel 'Range' = range     EXPORTING   ...

  3. linux编程问题记录

    1.程序中需要用到字符串的时候,尽可能选择string类型,这种类型的字符串有很多比较容易的功能,如字符串之间可以直接拷贝赋值 string a; string b="123"; ...

  4. NEFU 561 方块计算

    题目链接 简单搜索题 #include <cstdio> #include <iostream> #include <cstring> using namespac ...

  5. FreeRTOS学习及移植笔记之一:开始FreeRTOS之旅

    1.必要的准备工作 工欲善其事,必先利其器,在开始学习和移植之前,相应的准备工作必不可少.所以在开始我们写要准备如下: 测试环境:我准备在STM32F103平台上移植和测试FreeRTOS系统 准备F ...

  6. a0=1、a1=1、a2=a1+a0、a3=a2+a1,以此类推,请写代码用递归算出a30?

    public class Test { public static void main(String[] args) { System.out.println(recursive(30)); } pu ...

  7. Ubuntu下三个实用的录屏软件

    Ubuntu下三个实用的录屏软件 Kazam 优点: 易安装 可选择区域录制,也可全屏录制 有录屏和截图功能 安装: sudo apt-get install kazam 展示: Simple Scr ...

  8. 微信支付Native扫码支付模式二之CodeIgniter集成篇

    CI:3.0.5 微信支付API类库来自:https://github.com/zhangv/wechat-pay 请先看一眼官方场景及支付时序图:https://pay.weixin.qq.com/ ...

  9. fabric devenv Vagrantfile配置

    Vagrantfile文件只会在第一次执行vagrant up时调用执行,其后如果不明确使用vagrant reload,则不会被强制重新加载. # This is the mount point f ...

  10. Light oj1031 Easy Game (区间dp)

    题目链接:http://vjudge.net/contest/140891#problem/F A和B都足够聪明,只有我傻,想了好久才把代码和题意对应上[大哭] 代码: #include<ios ...