Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用DateTime.TryParseExact C#_C#_.net_Datetime_Tryparse - Fatal编程技术网

使用DateTime.TryParseExact C#

使用DateTime.TryParseExact C#,c#,.net,datetime,tryparse,C#,.net,Datetime,Tryparse,我不知道TryParseExact方法在示例日期格式中是如何工作的: 格式如下:(开始:2019.06.30.14:56:43) 如何告诉TryParseExact这种格式?根据中的两个混合答案,我建议您使用正则表达式去除所有非数字,然后解析剩余部分: var fromStr="Beginning: 2019.06.30. 14:56:43"; //strip non numerics fromStr = Regex.Replace(fromStr, @"[^0-9]", ""); //n

我不知道TryParseExact方法在示例日期格式中是如何工作的: 格式如下:(开始:2019.06.30.14:56:43
如何告诉TryParseExact这种格式?

根据中的两个混合答案,我建议您使用正则表达式去除所有非数字,然后解析剩余部分:

var fromStr="Beginning: 2019.06.30. 14:56:43";

//strip non numerics 
fromStr = Regex.Replace(fromStr, @"[^0-9]", "");

//now our dates of [2019. 09. 23. 14:54:23], [2019.09.23 14:54:23] or [2019-09-23 14:54:23]
//just become 20190923145423
if(DateTime.TryParseExact(fromStr, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, null, out var fromDt)
  //code if success
else
  //code if fail
文档和示例如下:

我主张清理一下你的数据,因为你以前建议过,你的约会时间可能会以古怪而奇妙的格式出现,到处都是随机文本

我在这里添加了另一个答案,因为其他答案中没有特别提到TryParseExact,但它的存在/使用可能是隐含的-这个问题非常具体,不同于那些需要直接回答的问题,但如果作为你“如此生活”的一部分,当你问了一个问题,然后又问了另一个问题,你在后一个问题中提到了前一个问题,这给了那些回答当前问题的人一些有用的背景知识,并允许你清楚地定义新问题与前一个问题的不同之处(如果你认为是的话)


我还将重申我以前的建议,即您确实应该将这些日期时间存储为日期时间,而不是各种格式的字符串。如果您正在解析日志输出,那么这是可以理解的,但是请尝试我在将其放入数据库的过程中而不是在退出的过程中获取它

关于MySQL中的数据的其他问题中没有涉及到这一点吗?请查看文档,找到所需的格式字符串,并将其全部放在一起;回到这里,问一个更具体的问题。