简体   繁体   English

DateTime Parse提取ss大于59时的秒数

[英]DateTime ParseExtract seconds when ss is more than 59

s_Time = DateTime.ParseExact("113388", "HHmmss", null);

Getting Error when ss is more than 59. How can avoid this. ss大于59时出现错误。如何避免这种情况。 If ss is more than 59 i want to display it as 00. 如果ss大于59,我想将其显示为00。

How can i achieve this? 我怎样才能做到这一点?

Actually i have a file name which contains time to validate. 其实我有一个文件名,其中包含验证时间。 ss will come randomly it may be 00 to 99, how ever i have to show 00 when its grater than 59 ss将随机出现,可能是00到99,当它比59刨丝器时,我怎么显示00

To follow up on my comment, something this makes the most sense to me. 为了跟进我的评论,这对我来说最有意义。 Treat the string as adding onto the hours and minutes that you parse. 将字符串视为添加到您解析的小时和分钟上。

var temp = "113388";
s_Time = DateTime.ParseExact(temp.Substring(0, 4
), "HHmm", null).AddSeconds(int.Parse(temp.Substring(4)));

I would only ever check this into production when the second hand says 88 though =) 当秒针说88时,我只会将其检入生产=)

Hexxagonal probably has the most sensible solution , but if you should for some reason want to replace the last part of the string (the seconds) with "00" when the value is above 59, and not update the minutes part, then this should work: Hexxagonal 可能拥有最明智的解决方案 ,但如果由于某些原因要替换的字符串为“00”时,该值高于59的最后一部分(秒),而不是更新分钟的一部分,那么这应该工作:

var input = "113388"; // original
var secsPart = input.Substring(4);
// Get 00 if over 59, else keep the seconds as they are:
var seconds = int.Parse(secsPart) > 59 ? "00" : secsPart; 
var editedInput = input.Substring(0,4) + seconds;

var s_Time = DateTime.ParseExact(editedInput, "HHmmss", null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM