-1

I am reading a text file to a list and, adding line numbers to the output. I want to search for a date in the format of MM/Day/Year and output it to console. If the next date in the text file is the same as the last one found then I do not want to output it, I only want to output the date that is later than the last. I am new to programming and have searched the site for date search code but haven't found something I understand. Here is my current code.

    internal class LineInformation
    {
        public int Index { get; set; }
        public string Line { get; set; }
    }
    static void Main(string[] args)
    {var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "doc.txt");

        var theWholeFile = File.ReadLines(fileName)
            .Select((line, index) => new LineInformation { Line = line, Index = index }).ToList();

Now I don't know how to search specifically for the aforementioned date format and write it to console. How would I declare a date format like MM/Day/Year then say something like;

    if(line.Contains(date)=>1stdate)
    {Console.WriteLine(date)} 
Oxx
  • 59
  • 2
  • 11
  • id probably start with regex for stripping down to lines with dates in, unless thats going to be all, and then converting the date regex found and comparing. – BugFinder Mar 20 '17 at 10:27
  • Thank you @BugFinder, I will look at regex and see what I can learn. I don't really understand what your saying but I will go and read-up on it. – Oxx Mar 20 '17 at 10:40
  • You haven't included input and desired output. I am guessing you just have to find datetime in each line (`Substring()` if position is known, `Split`+index/logic or regex otherwise), and then just [parse](http://stackoverflow.com/q/5366285/1997232) it and compare with instance of `DateTime()` (e.g. `var firstDate = DateTime.Today.AddDays(-1);`). – Sinatr Mar 20 '17 at 11:11
  • @Sinatr Sorry if my description is not very clear, I want to search a text file for dates in the format of mm/dd/yyy. I know how to read a text file and search for keywords but I don't know how to search for a date format of mm/dd/yyyy. There are a lot of repeated dates in the file but I only want all dates once (not repeated). Then when I `Console.WriteLine` I would like to see all dates without repeated dates. – Oxx Mar 20 '17 at 11:24
  • It may not be easy, best is if you show piece of this input file so someone can give you good suggestion. There should be a *pattern* to recognize datetime (e.g. generally you can try to use those too slashes: mm`/`dd`/`yyyy as well as knowledge such as before first slash is number between 01-12, then another one 01-31 and then 4 digit years. ) – Sinatr Mar 20 '17 at 11:32

1 Answers1

1

If you just need to compare the current date with the last one,and ignore the current if last one was greater then you can do it like this. Hope it helps

var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,     "doc.txt");
var theWholeFile = File.ReadLines(fileName)
    .Select((line, index) => new LineInformation { Line = line, Index = index}).ToList();
var lastValue = string.Empty;
foreach (var x in theWholeFile)
{
   //The first date will be printed no matter what
    if (lastValue == string.Empty)
    {
        Console.WriteLine(DateTime.ParseExact(x.Line,"mm/dd/yyyy", CultureInfo.InvariantCulture));
        lastValue = x.Line;
    }
    else
    {
        if (DateTime.Parse(x.Line) > DateTime.Parse(lastValue))
        {
            Console.WriteLine(DateTime.ParseExact(x.Line, "mm/dd/yyyy", CultureInfo.InvariantCulture));
        }
        lastValue = x.Line;
    }

}
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
SBO
  • 51
  • 4