Reminder Application C# (Mongodb) : 如何过滤mongodb中的数据并专门比较它们的日期时间和时间跨度并给出一个小消息框

Reminder Application C# (Mongodb) : How to filter the data from mongodb and compare them specially datetime and timespan and give a small message box

大家好,我是编码新手,我只是想得到一些关于创建简单的 c# winforms 提醒应用程序的帮助或建议

我想创建一个提醒应用程序,它用名称输入提醒的日期和时间 description.I 实际上我想用 timertick 来做这个,但我想先尝试一下,只用一个按钮来检查提醒时间和日期并与系统时间和日期进行比较,但我很难想出让它起作用的算法。最后,如果 reminderdate 和 remindertime 小于或等于 systemdate 和 system time 给出一个消息框,说 Reminder Alert 并删除比特定提醒。大部分都是我想出来的,但我坚持比较值的逻辑。

using MongoDB.Driver;
using System;
using System.Linq;
using System.Windows.Forms;

namespace MongoDBTest
{
    public partial class Reminder : Form
    {
        string connectionString = "mongodb://localhost:27017";
        public string databaseName = "MongoDB";
        public string collectionName = "Reminders";
        IMongoCollection<remindermodel> reminderCollection;
        public Reminder()
        {
            InitializeComponent();

            timepicker.Format = DateTimePickerFormat.Time;

            timepicker.ShowUpDown = true;

            datepicker.Format = DateTimePickerFormat.Custom;
            datepicker.CustomFormat = "MM/dd/yyyy";

            var client = new MongoClient(connectionString);
            var db = client.GetDatabase(databaseName);
            reminderCollection = db.GetCollection<remindermodel>(collectionName);
        }



        private void button1_Click(object sender, EventArgs e)
        {

            DateTime setdate = datepicker.Value; // TRYING TO SET date only here but cudnt
            DateTime datetoday = DateTime.Now;

            DateTime dt = this.timepicker.Value;


            var settime = dt.TimeOfDay;
            var set = settime.ToString();
            var currenttime = datetoday.TimeOfDay;

            

            if ((datetoday < setdate) && (currenttime < settime))
            {
                MessageBox.Show("Reminder Set!!");
                var remindermodel = new remindermodel
                {
                    remindername = aname.Text,
                    reminderdate = setdate,
                    remindertime = TimeSpan.Parse(set),
                    reminderdescription = reminderdesc.Text,
                };
                label9.Text = setdate.ToString();
                label8.Text = set;

                reminderCollection.InsertOneAsync(remindermodel);
            }
            else
            {
                MessageBox.Show("Reminder Cannot be set!!");
            }
            
        }



        private void button2_Click(object sender, EventArgs e)
        {
            DateTime datenow = DateTime.Now;
            var timenow = datenow.TimeOfDay;

            

            var filtertime = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
            var filterDefinition = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow) & filtertime;
            var projection = Builders<remindermodel>.Projection.Exclude("_id").Include("reminderdate").Include("remindertime");
            var reminders = reminderCollection.Find(filterDefinition).Project<remindermodel>(projection)    
                .FirstOrDefault();

            var remindersdatagrid = reminderCollection.Find(filterDefinition).Project<remindermodel>(projection)
                .ToList();

            datagridview.DataSource = reminders;


            if(reminders != null)
            {
                label7.Text = reminders.reminderdate.ToString();
                label8.Text = reminders.remindertime.ToString();
                MessageBox.Show("Reminder found");
            }
            else
            {
                MessageBox.Show("Sorry No reminders");
            }

        }

        
    }
}

我的提醒模型是

using System;

namespace MongoDBTest
{
    public class remindermodel
    {
        public string remindername { get; set; }

        public DateTime reminderdate { get; set; }

        public TimeSpan remindertime { get; set; }

        public string reminderdescription { get; set; }
    }
}

有人可以帮助我比较这些时间和日期的逻辑吗?因为即使从 datetimepicker 设置时间,它也会设置类似 05/09/2022 18 39 PM 的值。 我似乎无法摆脱最后一个时间部分,因为我是从时间选择器中单独获取时间的。

我会这样 it.To 使用 date 属性

比较日期
var time_comparison_result = TimeSpan.Compare(settime,currenttime);
var date_comparison_result = DateTime.Compare(setdate.Date,datetoday.Date);

                if ((date_comparison_result>0) && 
                    (time_comparison_result>0))
                {
                   //Set reminder
                }
                else
                {
                    //MessageBox.Show("Reminder Cannot be set!!");
                }

我建议将提醒日期和时间存储为 MongoDB 中的单个字段。这简化了比较,因为您只需比较一个字段:

public class remindermodel
{
    public string remindername { get; set; }
    public DateTime reminderdate { get; set; }
    // Not needed: public TimeSpan remindertime { get; set; }
    public string reminderdescription { get; set; }
}

那么,从UI中获取日期和时间时,可以先从DatePicker中获取日期,再从TimePicker中添加时间:

private void button1_Click(object sender, EventArgs e)
{
  DateTime setdate = datepicker.Value; 
  var timeOfDay = this.timepicker.Value.TimeOfDay;
  // .Date gets the date part from the DatePicker, 
  // Add adds the time that was selected in the TimePicker
  setDate = setDate.Date.Add(timeOfDay);
  // ... assign setDate to remindermodel.reminderdate later on

筛选提醒时,只需要比较时间字段:

private void button2_Click(object sender, EventArgs e)
{
  DateTime datenow = DateTime.Now;
  // Not needed anymore: var filtertime = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
  // If you want to show reminders of the past, change comparison to Lte (Less than or equal)
  var filterDefinition = Builders<remindermodel>.Filter.Gte(b => b.reminderdate, datenow);
  // ...