私は以下のコードを持っています。それはほとんどのシナリオでは機能しますが、今日私はexpiration_date
が09/30/2017 00:00:00
である1つのシナリオがstring
の10/15/2016 14:34:19
にあるcurrentDateTime
よりも小さいですか?私はちょうどstring
と比較して私の側のバグは何ですか?C#2日時を比較する正しい方法
System.DateTime expiration_date = newVer.License.Status.Expiration_Date;
DateTime currentDateTime = DateTime.Now;
currentDateTime.ToString("MM/dd/yyyy HH:mm:ss");
int a = expiration_date.ToString("MM/dd/yyyy HH:mm:ss")
.CompareTo(currentDateTime.ToString("MM/dd/yyyy HH:mm:ss"));
//MessageBox.Show("int a is :" + a);
if (expiration_date.ToString("MM/dd/yyyy HH:mm:ss")
.CompareTo(currentDateTime.ToString("MM/dd/yyyy HH:mm:ss")) < 1)
{
crossDate = 1;
MessageBox.Show("Cross Date Alert"+ " Expiry Date Is :"+
expiration_date.ToString("MM/dd/yyyy HH:mm:ss")
+ " "+"Current Date Is :"+
currentDateTime.ToString("MM/dd/yyyy HH:mm:ss"));
}
日付を文字列に変換するのではなく、直接比較する必要があります。比較前に文字列に変換することを依頼している場合は、増加する日付、つまりyyyy/MM/dd HH:mm:ss "' –
の形式を使用する必要があります。変換するには 'insist'する必要はありません2つの日付を比較する文字列に2つの日付。実際には変換のために「遅い」です。あなたが他のことができない限り、値のドメインを変更しないでください。 – shadow