gpt4 book ai didi

c# 将日期时间日期与时间跨度参数进行比较

转载 作者:行者123 更新时间:2023-11-30 16:55:28 25 4
gpt4 key购买 nike

目前我已经设法让我的程序正常工作,所以它比较两个日期时间日期,如果以前的记录早于或小于现在的时间,它会提醒最终用户患者需要检查。

我希望能够比较今天的日期和之前的日期,例如,如果距离最初的预订日期相隔一个月,它会执行相同的提醒。

private void PatientCheckUp()
{
SqlConnection connection = new SqlConnection();
Security security = new Security();
DateTime timenow = DateTime.Now.Date;
DateTime lastbooking = new DateTime();
string previousBooking = "";

try
{
connection.ConnectionString = connectionPath;
connection.Open();

SqlCommand cmd = new SqlCommand("SELECT Booking.Booking_Last_Booking, Booking.Booking_FkPatientId FROM Booking WHERE Booking.Booking_FkPatientId = @Patient_Id", connection);
cmd.Parameters.AddWithValue("@Patient_Id", cbopatientid.Text);
cmd.ExecuteNonQuery();

SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
previousBooking = security.Decrypt(dr["Booking_Last_Booking"].ToString(), security.GetPassword());
}
}

lastbooking = Convert.ToDateTime(previousBooking);

/*Currently checks if the previous booking date is less than or before today, if so it alerts the user */
if (lastbooking.Date < timenow.Date)
{
MessageBox.Show("Patient requires a checks up");
}
}
catch (SqlException sql)
{
MessageBox.Show(sql.Message);
}
finally
{
connection.Close();
connection.Dispose();
}
}

最佳答案

using System;

public class Program
{
public static void Main()
{
DateTime dt = new DateTime();
DateTime dt2 = new DateTime();

dt = DateTime.Now;
dt2 = dt.AddDays(20);


TimeSpan ts = dt2.Subtract(dt);

Console.WriteLine(dt.ToString());
Console.WriteLine(dt2.ToString());

if(ts.Days > 30)
{
Console.WriteLine("It has been at least a month since last check up");
}
else
{
Console.WriteLine("It has been "+ ts.Days+" days since last check up");
}

}
}

以上是我整理的一段示例代码,用于说明您如何能够做到这一点。您可以确定一个“月”应该是多少天,然后在它超过您指定的阈值时采取行动。

Here是我正在玩的 DotNetFiddle

关于c# 将日期时间日期与时间跨度参数进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29415840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com