myl.Add(AllNews[i].original_time);
myl是列表类型的字符串,而且original_time是字符串。
例如,现在original_time包含字符串:"D140707T2149"
现在,我想将此字符串转换为仅包含D和T的数字,并格式化为:yyyyMMddHHmm
不包含秒。
然后最后构建一个像这样的字符串:
string results = myTime.ToString("hh:mm דווח במקור בתאריך : dd.MM.yy : שעה");
希伯来语单词保持不变,只是日期和时间每次都会改变。
这就是我试图做的:
IFormatProvider provider = CultureInfo.InvariantCulture;
DateTime myTime = DateTime.ParseExact(AllNews[i].original_time, "DyyMMddThhmm", provider);
string results = myTime.ToString("hh:mm דווח במקור בתאריך : dd.MM.yy : שעה");
但是我在网上遇到了异常:
DateTime myTime = DateTime.ParseExact(AllNews[i].original_time, "DyyMMddThhmm", provider);
字符串未被识别为有效的DateTime
然后,我首先尝试从字符串中删除D
and T
:
AllNews[i].original_time = Regex.Replace(AllNews[i].original_time, "[^0-9]", "");
IFormatProvider provider = CultureInfo.InvariantCulture;
DateTime myTime = DateTime.ParseExact(AllNews[i].original_time, "yyyyMMddHHmm", provider);
string results = myTime.ToString("hh:mm דווח במקור בתאריך : dd.MM.yy : שעה");
但是还是和以前一样的例外:
System.FormatException was unhandled
HResult=-2146233033
Message=String was not recognized as a valid DateTime.
Source=mscorlib
StackTrace:
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)
at ScrollLabelTest.ListsExtractions.listtostringlist(List`1 lnl, List`1 myl) in ListsExtractions.cs:line 345
at ScrollLabelTest.ListsExtractions.Ext(String filename) in
ListsExtractions.cs:line 220
at ScrollLabelTest.Form1..ctor() in Form1.cs:line 127
at ScrollLabelTest.Program.Main() in Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
HH
由于在以下情况下使用24h clock(21
),因此您需要在小时中使用大写字母:
string original_time = "D140707T2149";
所以这有效:
DateTime myTime = DateTime.ParseExact(original_time, "DyyMMddTHHmm", CultureInfo.InvariantCulture);
请参阅:http : //msdn.microsoft.com/zh-cn/library/8kb3ddd4(v=vs.110).aspx#HH_Specifier
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句