简体   繁体   English

输入字符串格式不正确无法解析为DateTime

[英]Input string not in correct format to parse into DateTime

I am facing problem while converting DataTime into Time on 12 hour clock machine. 我在12小时时钟机上将DataTime转换为时间时遇到问题。 Following code works fine on 24 Hour clock machine. 以下代码在24小时时钟机上正常工作。

(new DisplayReminder(_name, _displayText, _snoozTime, TimeSpan.Parse(_startTime.ToShortTimeString(), CultureInfo.InvariantCulture))).Show();

TimeSpan.Parse(_startTime.ToShortTimeString() throws exeception that input string not in correct format, here I am trying to get time part from DateTime value _startDate Any suggestion or solution on this problem. TimeSpan.Parse(_startTime.ToShortTimeString()抛出异常输入字符串格式不正确,这里我试图从DateTime值获取时间部分_startDate对此问题的任何建议或解决方案。

It's not clear what you're trying to do, but just getting the time of day shouldn't involve string conversions: 目前还不清楚你要做什么,但只是获取时间不应该涉及字符串转换:

TimeSpan time = _stateTime.TimeOfDay;

I'd strongly advise you to avoid string conversions unless they're inherently part of what you're trying to achieve. 我强烈建议你避免字符串转换,除非它们本身就是你想要达到的目标的一部分。

Personally I don't like using TimeSpan as a time of day anyway, but that's the BCL for you. 就个人而言,我不喜欢使用TimeSpan作为时间,但那是你的BCL。 You might want to also look into my Noda Time library which has a clearer separation of various date/time concepts. 您可能还想查看我的Noda Time库,它可以更清晰地分离各种日期/时间概念。

Try this: 试试这个:

TimeSpan.ParseExact(
         _startTime.ToString("hh:mm:ss"), "hh:mm:ss",
                          System.Globalization.CultureInfo.InvariantCulture);

it will format your date to same format as TimeSpan.ParseExact accepts, so it will run on any machine (I assume that _startTime is DateTime ) 它会将你的日期格式化为TimeSpan.ParseExact接受的相同格式,因此它将在任何机器上运行(我假设_startTime是DateTime

尝试

startTime.TimeOfDay.ToString()

I am pretty sure that it works fine when 24H but crushes in 12H system, BECAUSE of the "PM" "AM" part ! 我很确定它在24小时工作正常但在12H系统中崩溃,因为“PM”“AM”部分! get rid of it you'll be safe 11:54:33 PM and 23:54:33 are VERY different in terms of handling 摆脱它你将是安全的11:54:33 PM和23:54:33在处理方面是非常不同的

EDIT it might be not the best solution but it works if you cut the last indexes of the string: 编辑它可能不是最好的解决方案,但如果你剪切字符串的最后一个索引它是有效的:

    int index = _StartTime.IndexOf("M");
    if (index >= 0) 
{
_StartTime = _StartTime.Substring(0, index-1);
switch (_StartTime.IndexOf("P"))
case : -1
 _StartTime = _StartTime.Substring(0,_StartTime.Length);
 break;

default:
 string hours = _startTime.Substring(_StartTime.Length-8,2);
 int H = Convert.ToInt32(hours);
 H += 12;
 string result = _StartTime.Substring(0, _StartTime.Length-8)+ Convert.ToString(H)+_startTime.Substring(_StartTime.Length-6);

_StartTime = result;
break;
}

this will throw AM/PM away, you can use the same previous code after this SECOND EDIT it's the most horrible solution but it works ;) 这将抛出AM / PM,你可以使用相同的先前代码在这次SECOND EDIT之后这是最可怕的解决方案,但它有效;)

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

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