how to compare two dates in datetimepicker

前端 未结 2 1003
谎友^
谎友^ 2021-01-24 04:59

I am doing Windows project, in which I have two DateTimePicker controls, one for StartDate and other for EndDate.

In runtime, when user selects the StartDate and EndDate

相关标签:
2条回答
  • 2021-01-24 05:24

    I would recommend using DateTime.TryParse (doc here) instead of just converting. That will allow you to provide valid formatting, handle failures (convert will throw if it fails).

    The part about using the Date property seems fine, and comparison operators will work as expected.

    0 讨论(0)
  • 2021-01-24 05:31

    Try and deal with DateTime datatypes here rather than converting everything to string. After reading the line from the file, parse out the date. Assuming it is always going to be the first element in the file, and is followed by a space, you could do this by using DateTime lineDate = DateTime.Parse(line.Split(" ")[0]);

    Once you have the date from the file in that format, and you have DateTime fromDate and DateTime toDate obtained from your date pickers, you can write if(lineDate >= fromDate && lineDate <= toDate) sb.AppendLine(line);

    0 讨论(0)
提交回复
热议问题