Try this function
private static DateTime? GetTime(string s)
{
var regex = new Regex(
@"^[\-drswx]{10}\s+\d+\s+ftp\s+ftp\s+\d+\s+" +
@"(?<Month>[a-zA-Z]{3})\s+(?<Day>\d{1,2})\s+" +
@"(?<Hours>\d{2})\:(?<Mins>\d{2})\s+.*$");
var m = regex.Match(s);
DateTime? result = null;
if (m.Success)
{
var year = DateTime.Now.Year.ToString();
var month = m.Groups["Month"].ToString();
var day = m.Groups["Day"].ToString();
var hours = m.Groups["Hours"].ToString();
var mins = m.Groups["Mins"].ToString();
var timepart = string.Format(@"{0}/{1}/{2} {3}:{4}", year, month, day, hours, mins);
result = DateTime.ParseExact(timepart, "yyyy/MMM/dd HH:mm"
, new CultureInfo("en-US"));
if (result.Value.Month > DateTime.Now.Month)
{
result.Value.AddYears(-1);
}
}
else
{
regex = new Regex(
@"^[\-drswx]{10}\s+\d+\s+ftp\s+ftp\s+\d+\s+" +
@"(?<Month>[a-zA-Z]{3})\s+(?<Day>\d{1,2})\s+" +
@"(?<Year>\d{4})\s+.*$");
m = regex.Match(s);
if (!m.Success)
{
return null;
}
var year = m.Groups["Year"].ToString();
var month = m.Groups["Month"].ToString();
var day = m.Groups["Day"].ToString();
var timepart = string.Format(@"{0}/{1}/{2}", year, month, day);
result = DateTime.ParseExact(timepart, "yyyy/MMM/dd", new CultureInfo("en-US"));
}
return result;
}