我怎么能格式化一个字符串与10位数字和2个字母yyyyMMddHHmm,然后格式化为另一个字符串

本文关键字:字符串 格式化 2个 yyyyMMddHHmm 另一个 然后 10位 怎么能 一个 数字 | 更新日期: 2023-09-27 18:13:43

myl.Add(AllNews[i].original_time);

myl是一个List类型的字符串,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);

字符串未被识别为有效的日期时间

然后我首先尝试从字符串中删除DT:

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: 

我怎么能格式化一个字符串与10位数字和2个字母yyyyMMddHHmm,然后格式化为另一个字符串

由于您使用的是24小时制时钟(21),因此需要大写HH来表示小时:

string original_time = "D140707T2149";

DateTime myTime = DateTime.ParseExact(original_time, "DyyMMddTHHmm", CultureInfo.InvariantCulture);

见:http://msdn.microsoft.com/en-us/library/8kb3ddd4 (v = vs.110) . aspx # HH_Specifier

您需要使用DT文字字符串分隔符作为'或".

您还需要使用HH说明符而不是hh说明符,因为您的小时是24小时时钟

DateTime myTime = DateTime.ParseExact("D140707T2149" ,
                                      "'D'yyMMdd'T'HHmm",
                                      CultureInfo.InvariantCulture);
Console.WriteLine(myTime);

这里a demonstration

hh表示0112 (12小时时钟), HH表示0023