如何比较的DateTimePicker两个日期 [英] how to compare two dates in datetimepicker

查看:140
本文介绍了如何比较的DateTimePicker两个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做Windows项目,其中我有两个的DateTimePicker控件,一个是开始日期和其他的结束日期

i am doing windows project, in which i have two datetimepicker controls, one for startdate and other for enddate

在运行时,当用户选择的开始日期和结束日期从控制,它应该阅读文本文件(IE)的log.txt&安培;通过线搜索行获得这两个日期和以及其间日期的匹配和将数据写入一个文本框或标签控件

In runtime, when user selects the startdate and enddate from that controls, it should read the textfile(i.e) log.txt & search line by line get the matching of both these dates and as well as inbetween dates and write the data to a textbox or label control

有关例如,在日志文件中的一些数据就像下面

for eg in log file the some data is like below

2013年3月12日下午2点51分47秒 - ASDASDASD.D20131203145019
2013年4月12日下午2时52分23秒 - ASDDFSDSA .C20131203145019
2013年5月12日下午2点52分37秒 - SDASAFAS_20131203182101.D
2013年6月12日下午3点17分11秒 - RRRTWEWA_20131203184602.D00
2013年7月12日3 :35:32 PM - XBCNXCXCXC.D0120131203153408

3/12/2013 2:51:47 PM - ASDASDASD.D20131203145019 4/12/2013 2:52:23 PM - ASDDFSDSA.C20131203145019 5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D 6/12/2013 3:17:11 PM - RRRTWEWA_20131203184602.D00 7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408

如果我从2013年12月5日搜到2013年12月7日

if i search from 5 dec 2013 to 7 dec 2013

它应该找回

2013年5月12日下午2点52分37秒 - SDASAFAS_20131203182101.D
2013年6月12日下午3时17分十一秒 - RRRTWEWA_20131203184602 .D00
2013年7月12日下午3时35分32秒 - XBCNXCXCXC.D0120131203153408

5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D 6/12/2013 3:17:11 PM - RRRTWEWA_20131203184602.D00 7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408

但uptonow我得到

but uptonow i am getting

2013年5月12日下午2点52分37秒 - SDASAFAS_20131203182101.D
2013年7月12日下午3时35分32秒 - XBCNXCXCXC.D0120131203153408

5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D 7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408

我正在retriving只有STARTDATE&放大器;结束日期匹配数据未插图中最新的数据

i am retriving only the startdate & enddate matching data not inbetween date data

以下是一些编码我都试过的。

The below are some of the coding i have tried

string FDDate = FD.Date.ToString("M/d/yyyy");
                string TDDate = TD.Date.ToString("M/d/yyyy");

                string searchstring = EnterFileNameTextbox.Text.ToString();
                string searchfromdate = FromDateTimePicker.Value.ToShortDateString();
                string searchtodate = ToDateTimePicker.Value.ToShortDateString();

                string line;
                StringBuilder sb = new StringBuilder();
                int counter = 0;
                using (StreamReader file = new StreamReader(@"D:\log.txt"))
                {
                    while ((line = file.ReadLine()) != null)
                    {
                        if (line.Contains(searchstring) && line.Contains(FDDate))
                        {
                            sb.AppendLine(line.ToString());
                            counter++;
                        }                      

                        else if (line.Contains(searchstring) && !line.Contains(FDDate))
                        {
                            if (FD.Date < TD.Date)
                            {                                
                                sb.AppendLine(line.ToString());
                                counter++;
                                FDDate = FD.Date.AddDays(1).ToShortDateString();
                            }
                            else if (FD.Date == TD.Date)
                            {
                                FDDate = FD.Date.ToShortDateString();
                                sb.AppendLine(line.ToString());
                                counter++;
                            }
                        }
                    }
                }   

                ResultTextBox.Text = sb.ToString();
                CountLabel.Text = counter.ToString();
            }

            catch (Exception ex)
            {
                MessageBox.Show("The file could not be read");
                MessageBox.Show(ex.Message);
            }



我没有得到主意,做循环

i am not getting idea to do the loop

推荐答案

我会建议使用 DateTime.TryParse (文件的这里)而不只是转换。 。这将允许您提供有效的格式,处理故障(转换如果失败会抛出)

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.

这篇关于如何比较的DateTimePicker两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆