从一个特定的时间格式的总秒计算 [英] Calculation of Total seconds from a particular format of time

查看:107
本文介绍了从一个特定的时间格式的总秒计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net C#


解决方案 '33小时40分钟40秒总秒>

如果你给出的格式33小时40分钟40秒的字符串,你必须先分析字符串。

 变种s =33小时40分钟40秒; 
变种匹配= Regex.Matches(S,\d +);
变种小时= Convert.ToInt32(比赛[0]);
变种分钟= Convert.ToInt32(比赛[1]);
变种秒= Convert.ToInt32(比赛[2]);
VAR totalSec =小时* 3600 +分钟* 60 +秒;

这代码,很明显,检查涉及任何错误。所以,你可能想要做这样的事情,确保3场比赛被发现了,那比赛是分秒等有效的值。


How to calculate total seconds of '33 hr 40 mins 40 secs' in asp.net c#

解决方案

If you're given a string of the format "33 hr 40 mins 40 secs", you'll have to parse the string first.

var s = "33 hr 40 mins 40 secs";
var matches = Regex.Matches(s, "\d+");
var hr = Convert.ToInt32(matches[0]);
var min = Convert.ToInt32(matches[1]);
var sec = Convert.ToInt32(matches[2]);
var totalSec = hr * 3600 + min * 60 + sec;

That code, obviously, has no error checking involved. So you might want to do things like make sure that 3 matches were found, that the matches are valid values for minutes and seconds, etc.

这篇关于从一个特定的时间格式的总秒计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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