Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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
C# 从字符串中提取复杂的日期/时间格式_C#_String_Datetime - Fatal编程技术网

C# 从字符串中提取复杂的日期/时间格式

C# 从字符串中提取复杂的日期/时间格式,c#,string,datetime,C#,String,Datetime,我正试图找到最好的方法,从从FTP文件列表中检索到的文件名字符串中提取以非常奇怪的格式存储的日期和时间字符串 字符串如下所示: -rwxr-xr-x 1 ftp ftp 267662 Jun 06 09:13 VendorInventory_20130606_021303.txt\r 我试图提取的具体数据是20130606_021303。021303的格式为小时、秒和毫秒。Parse和DateTime.ParseExact不愿意合作。你知道如何启动并运行它吗?更新

我正试图找到最好的方法,从从FTP文件列表中检索到的文件名字符串中提取以非常奇怪的格式存储的日期和时间字符串

字符串如下所示:

-rwxr-xr-x    1 ftp      ftp        267662 Jun 06 09:13 VendorInventory_20130606_021303.txt\r
我试图提取的具体数据是20130606_021303。021303的格式为小时、秒和毫秒。Parse和DateTime.ParseExact不愿意合作。你知道如何启动并运行它吗?

更新我假设FTP列表的文件显示有一个固定的结构,所以你可以简单地使用String.Substring提取datetime字符串,然后用datetime.ParseExact进行解析:

原始答案

使用正则表达式。请尝试以下操作:

var s = "-rwxr-xr-x    1 ftp      ftp        267662 Jun 06 09:13 VendorInventory_20130606_021303.txt\r";

/* 
   The following pattern means:
   \d{8})    8 digits (\d), captured in a group (the parentheses) for later reference
   _          an underscore
   (\d{6})    6 digits in a group
   \.         a period. The backslash is needed because . has special meaning in regular expressions
   .*         any character (.), any number of times (*)
   \r         carriage return
   $          the end of the string
*/
var pattern = @"(\d{8})_(\d{6})\..*\r$";

var match = Regex.Match(s, pattern);
string dateString = matches.Groups[1].Value;
string timeString = matches.Groups[2].Value;
并使用ParseExact进行解析:


看起来您已经获得了文件列表的整行,包括权限、用户、所有者、文件大小、时间戳和文件名

您请求的数据似乎只是文件名的一部分。使用一些基本的字符串操作拆分、子字符串等。。。第一然后,当您只有datetime部分时,就可以调用datetime.ParseExact

先自己试一试。如果遇到问题,请更新您的问题以显示您正在尝试的代码,有人会进一步帮助您

哦,很好。搞什么鬼。我感到很慷慨。这是一条单行线:

string s = // your string as in the question

DateTime dt = DateTime.ParseExact(string.Join(" ", s.Split('_', '.'), 1, 2),
                                  "yyyyMMdd HHmmss", null);
但是,请下次先自己尝试一下。

这可能会奏效:

string s = "-rwxr-xr-x 1 ftp ftp 267662 Jun 06 09:13 VendorInventory_20130606_021303.txt\r";

// you might need to adjust the IndexOf method a bit - if the filename/string ever changes...
// or use a regex to check if there's a date in the given string 

// however - the first thing to do is extract the dateTimeString:
string dateTimeString = s.Substring(s.IndexOf("_") + 1, 15);

// and now extract the DateTime (you could also use DateTime.TryParseExact)
// this should save you the trouble of substringing and parsing loads of ints manually :)
DateTime dt = DateTime.ParseExact(dateTimeString, "yyyyMMdd_hhmmss", null);
我没有想到将null传递给ParseExact。@ZevSpitz-为提供程序传递null告诉它使用当前区域性。您可以传递CultureInfo.InvariantCulture。这无关紧要,因为在这个特定的格式字符串中没有特定于区域性的项。
string s = // your string as in the question

DateTime dt = DateTime.ParseExact(string.Join(" ", s.Split('_', '.'), 1, 2),
                                  "yyyyMMdd HHmmss", null);
string s = "-rwxr-xr-x 1 ftp ftp 267662 Jun 06 09:13 VendorInventory_20130606_021303.txt\r";

// you might need to adjust the IndexOf method a bit - if the filename/string ever changes...
// or use a regex to check if there's a date in the given string 

// however - the first thing to do is extract the dateTimeString:
string dateTimeString = s.Substring(s.IndexOf("_") + 1, 15);

// and now extract the DateTime (you could also use DateTime.TryParseExact)
// this should save you the trouble of substringing and parsing loads of ints manually :)
DateTime dt = DateTime.ParseExact(dateTimeString, "yyyyMMdd_hhmmss", null);