我怎样才能看到最接近一个的时间是我的列表?

Jer*_*emy 0 .net c# datetime

我正在为我的手机开发一个应用程序,您可以在其中查看使用列表框输入时间的总线时间,然后检查列表,然后向您显示最接近它的时间或完全匹配.我已经尝试过自己并且运气好了我在这里发现的一些代码,但我仍然无法让它工作.

这里

    public  MainPage()
    {
        InitializeComponent();

        List<string> thedates = new List<string>();



        thedates.Add("0130");
        thedates.Add("0230");
        thedates.Add("0330");
        thedates.Add("0430");
        thedates.Add("0530");


        DateTime fileDate, closestDate;


        int min = int.MaxValue;

        foreach (DateTime date in theDates)
            if (Math.Abs(date.Ticks - fileDate.Ticks) < min)
            {
                min = date.Ticks - fileDate.Ticks;
                closestDate = date;
            }
    }
Run Code Online (Sandbox Code Playgroud)

错误:当前上下文中不存在名称"theDates".

对不起,如果这是简单或复杂的事情.任何帮助表示赞赏.

Jon*_*arr 5

改变'theDates'

foreach (DateTime date in theDates)
Run Code Online (Sandbox Code Playgroud)

''thedates'.

如上所述 - 您也没有使用正确的对象.您应该只创建一个DateTime对象列表而不是字符串.

List<DateTime> thedates = new List<DateTime>();

thedates.Add(new DateTime{ // Set up values here });
..
..
Run Code Online (Sandbox Code Playgroud)