2015-04-02 165 views
1

目前我設法讓我的程序工作,因此它比較了兩個日期時間日期,如果以前的記錄是現在或現在的時間,它會提醒最終用戶,病人需要檢舉。c#比較日期時間日期與時間跨度參數

我希望能夠比較今天的日期和以前的日期,如果自最初預訂日期起,例如有一個月相隔一個月,它將執行相同的警報。

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(); 
    } 
} 
+0

您是否實際使用TimeSpan span = new TimeSpan(1,2,0,30,0); ?或者您的問題標題中的措辭不正確地暗示您使用它? – 2015-04-02 14:58:25

+0

我的意思並不是這個意思,但如果需要計算工作,那麼我肯定會使用它 – 10AlexD10 2015-04-02 15:25:42

+0

不需要TimeSpan數據類型,下面的任何一種解決方案都應該做到這一點。附:參數也是FUNCTIONNAME(參數1,參數2) – 2015-04-02 15:26:59

回答

0
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是我玩弄

0

你可以做類似下面的DotNetFiddle。

if (timenow.Date - lastbooking.Date).Days > 30) 
{ 
    MessageBox.Show("It's been over 30 days since last checkup."); 
}