Reputation: 3
I have tried with Ftp Web Response to access files and directory details in c# with ASP.Net.
System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
I got the response like below from FTP server:
01-27-17 06:54AM 14613 A J DOHMANN CHRYSLER INC.csv
09-20-18 12:27PM 122816576 ABC1Append.csv
09-12-18 08:45AM 54998269 ABC1_FileForAppend.csv
When i tried to format the date time with below regular expression, i getting the result as "01-01-0001 12:00 AM"
regular expression:
(?<timestamp>\\d{2}\\-\\d{2}\\-\\d{2}\\s+\\d{2}:\\d{2}[Aa|Pp][mM])\\s+(?<dir>\\<\\w+\\>){0,1}(?<size>\\d+){0,1}\\s+(?<name>.+)
Please some one help in this, how to get proper date time format from FTP web response.
TIA.
Upvotes: 0
Views: 542
Reputation: 131364
No repro.
That regex does capture the timestamps. The following code :
var input=@"01-27-17 06:54AM 14613 A J DOHMANN CHRYSLER INC.csv
09-20-18 12:27PM 122816576 ABC1Append.csv
09-12-18 08:45AM 54998269 ABC1_FileForAppend.csv";
var pattern="(?<timestamp>\\d{2}\\-\\d{2}\\-\\d{2}\\s+\\d{2}:\\d{2}[Aa|Pp][mM])\\s+(?<dir>\\<\\w+\\>){0,1}(?<size>\\d+){0,1}\\s+(?<name>.+)";
var rex=new Regex(pattern);
var ts=rex.Match(input).Groups["timestamp"].Value;
Returns
01-27-17 06:54AM
I suspect some other code is trying to parse this string, fails and returns a default DateTime value.
This string can be parsed eg with :
DateTime.Parse(ts,CultureInfo.GetCultureInfo("en-US"));
or
if(DateTime.TryParse(ts,CultureInfo.GetCultureInfo("en-US"),DateTimeStyles.None,out var dt))
{
Console.WriteLine(dt);
}
else
{
//Parsing failed!
}
Upvotes: 0