首页 \ 问答 \ 解析TimeSpan大于24小时?(Parse a TimeSpan greater than 24 hours? [duplicate])

解析TimeSpan大于24小时?(Parse a TimeSpan greater than 24 hours? [duplicate])

这个问题在这里已有答案:

我有这个功能:

  public static decimal Hours(string s)
    {    
        return (decimal)TimeSpan.Parse(s).TotalHours + 0.005M;
    }

格式为小时:分钟,例如:30:20或10:17。 如果我写的话就会失败,因为它超过24小时就会说25:03或30:20。 有没有办法让它解析> 23:59? 简单地说,如果我的字符串是30:30,这个函数需要返回30.5。


This question already has an answer here:

I have this function:

  public static decimal Hours(string s)
    {    
        return (decimal)TimeSpan.Parse(s).TotalHours + 0.005M;
    }

The format is Hours:Minutes, ex: 30:20, or 10:17. It fails if I write give it say 25:03 or 30:20 since it is over 24 hours. Is there a way I can have it parse something > 23:59 ? Simply put, if my string is 30:30, this function needs to return 30.5.


原文:https://stackoverflow.com/questions/15751325
更新时间:2019-12-13 07:38

最满意答案

我不确定是否可以使用标准的TimeSpan.Parse()方法完成,但你可以这样做:

public static decimal Hours(string s)
{
    decimal r;
    if (decimal.TryParse(s, out r))
        return r;

    var parts = s.Split(':');
    return (decimal)new TimeSpan(int.Parse(parts[0]), int.Parse(parts[1]),0).TotalHours;
}

I'm not sure it can be done using standard TimeSpan.Parse() method, but you can do that:

public static decimal Hours(string s)
{
    decimal r;
    if (decimal.TryParse(s, out r))
        return r;

    var parts = s.Split(':');
    return (decimal)new TimeSpan(int.Parse(parts[0]), int.Parse(parts[1]),0).TotalHours;
}
2013-04-01

相关问答

更多

如何减少android SQLite中的自动增量_id?(How to decrease the Auto increment _id in android SQLite?)

编辑:也许我应该说清楚只是插入具有正确id的行而不是操纵序列号肯定比下面的方法更好。 如果表中没有id = 3的行,则即使在AUTOINCREMENT表中也可以在id中插入固定值。 也就是说,如果您确定 ,可以使用自动增量值设置为任意值; UPDATE sqlite_sequence set seq=<next sequence no -1> where name=<table name>; 也就是说,如果你想在表'TableA'上的下一个插入中使用AUTOINCREMENT来生成5,那么你可

脚本拼图

最新问答

更多
  • NumPy标量,广播和__iadd__方法(NumPy scalars, broadcasting and the __iadd__ method)
  • 快速排序算法输出错误(Getting wrong output for quicksort algorithm)
  • Rails管理员:没有Rails 4应用程序的编辑,显示或删除按钮(Rails Admin: No edit, show, or delete buttons for Rails 4 app)
  • 部分刷新问题触发两次(Issue with partial refresh triggered twice)
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • EF6.1 beta 1如何获得Tooling Consolidation功能?(EF6.1 beta 1 how to get the Tooling Consolidation feature?)
  • 适用于Windows和PHP 5.5的Phalcon(Phalcon for Windows and PHP 5.5)
  • 有没有办法忽略hg责任中的提交?(Is there a way to ignore a commit in hg blame?)
  • Desire2通过API学习访问学期的权限(Desire2Learn Permissions to Access Semesters Via API)
  • 从aspx将服务器的文件复制到另一台服务器(Copy a file of a server to another server from aspx)
  • 使用YSlow / Web Developer扩展程序确定页面大小(Figuring out page size with YSlow / Web Developer extension)
  • Select语句将字符串中的前7个字符与其他表中的其他7个字符进行比较(Select statement which compares first 7 chars in a string with other 7 chars in other table)
  • 检测对注册表的更改,防止更改等(Detect changes to registry, prevent changes, etc)
  • 用户的鼠标/键盘运动分析是否有特殊名称?(Does the analysis of mouse/keyboard motions of a user have a special name?)
  • 获取Rails中资源的路由列表(Get list of routes for a resource in Rails)
  • 用函数值除以数字(Divide a function value by a number)
  • 熊猫Python:如何从今天开始创建失效列?(Pandas Python: How to create lapse since today column?)
  • GDK Bug报告(GDK Bug Reports)
  • 困惑于在python中循环遍历列表(confused about looping through lists in python)
  • 'less'由'which'输出指定的文件('less' the file specified by the output of 'which')
  • 如何在ASP.NET中优化AJAX(How to optimize AJAX in ASP.NET)
  • 在文本文件中转换纪元时间(Convert epoch times in text file)
  • 表细胞伸展(Table cell stretching)
  • GridView控件(GridView Control)
  • 从构建定义文件控制pdb文件输出(Control pdb file output from build defintion file)
  • *将方法传递给类(*Passing a Method into a Class)
  • 如何在HttpReceiveHttpRequest()中使用HTTPS?(How to use HTTPS with HttpReceiveHttpRequest()?)