如何比较 C # 中的日期时间?

我不希望用户给回日期或时间。

如果输入的日期和时间少于当前时间,我如何比较?

如果当前的日期和时间是17-Jun-2010,12:25 PM,我希望用户不能给日期之前6月17日 -2010和时间之前12:25 PM。

像我的函数返回假如用户输入的时间是16-Jun-2010和时间12:24 PM

303277 次浏览

日期时间。比较

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;


if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";


Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
// The example displays the following output:
//    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM

微软还实现了操作符“ <’和’>”,所以您可以使用它们来比较两个日期。

if (date1 < DateTime.Now)
Console.WriteLine("Less than the current time!");

MuSTaNG 的回答 说明了一切,但我仍然添加了它,以使其更加详细,包括链接和所有内容。


传统的操作员

DateTime是可用的,因为。NET Framework 1.1.此外,使用常规操作符 +-也可以对 DateTime对象进行加减运算。

来自 MSDN 的一个例子:

平等:
System.DateTime april19 = new DateTime(2001, 4, 19);
System.DateTime otherDate = new DateTime(1991, 6, 5);


// areEqual gets false.
bool areEqual = april19 == otherDate;


otherDate = new DateTime(2001, 4, 19);
// areEqual gets true.
areEqual = april19 == otherDate;

其他操作符也可以同样使用。

下面是 DateTime可用的 所有接线员列表。

//Time compare.
private int CompareTime(string t1, string t2)
{
TimeSpan s1 = TimeSpan.Parse(t1);
TimeSpan s2 = TimeSpan.Parse(t2);
return s2.CompareTo(s1);
}
public static bool CompareDateTimes(this DateTime firstDate, DateTime secondDate)
{
return firstDate.Day == secondDate.Day && firstDate.Month == secondDate.Month && firstDate.Year == secondDate.Year;
}

在一般情况下 你需要比较 DateTimes和相同的 Kind:

if (date1.ToUniversalTime() < date2.ToUniversalTime())
Console.WriteLine("date1 is earlier than date2");

MSDNDateTime.Compare的解释(这也适用于 ><==等操作员) :

为了确定 t1与 t2的关系,Compare 方法比较 T1和 t2的 Ticks 属性,但是 忽略他们的类属性。 在比较 DateTime 对象之前,请确保这些对象表示 在同一时区的时间。

因此,在处理以不同时区表示的 DateTimes时,一个简单的比较可能会产生意想不到的结果。

如果有两个看起来相同的 DateTime,但 Compare 或 Equals 没有返回您所期望的值,这就是如何比较它们的方法。

下面是一个精度为1毫秒的例子:

bool areSame = (date1 - date2) > TimeSpan.FromMilliseconds(1d);

下面是 Unity 环境中一个典型的简单示例

using UnityEngine;


public class Launch : MonoBehaviour
{
void Start()
{
Debug.Log("today " + System.DateTime.Now.ToString("MM/dd/yyyy"));


// don't allow the app to be run after June 10th
System.DateTime lastDay = new System.DateTime(2020, 6, 10);
System.DateTime today = System.DateTime.Now;


if (lastDay < today) {
Debug.Log("quit the app");
Application.Quit();
}
UnityEngine.SceneManagement.SceneManager.LoadScene("Welcome");
}
}
using System;
//
public enum TimeUnit : byte {
Unknown = 0x00, //
Nanosecond = 0x01, // ns, not available in DateTime
Millisecond = 0x02, // ms
Second = 0x04, // sec
Minute = 0x08, // min
Hour = 0x10, // h
Day = 0x20, // d
Month = 0x40, // M
Year = 0x80, // Y
AllDate = TimeUnit.Year | TimeUnit.Month | TimeUnit.Day,
AllTime = TimeUnit.Hour | TimeUnit.Minute | TimeUnit.Second,
UpToNanosecond = TimeUnit.Nanosecond | TimeUnit.Millisecond | TimeUnit.Second | TimeUnit.Minute | TimeUnit.Hour | TimeUnit.Day | TimeUnit.Month | TimeUnit.Year,
UpToMillisecond = TimeUnit.Millisecond | TimeUnit.Second | TimeUnit.Minute | TimeUnit.Hour | TimeUnit.Day | TimeUnit.Month | TimeUnit.Year,
UpToSecond = TimeUnit.Second | TimeUnit.Minute | TimeUnit.Hour | TimeUnit.Day | TimeUnit.Month | TimeUnit.Year,
UpToMinute = TimeUnit.Minute | TimeUnit.Hour | TimeUnit.Day | TimeUnit.Month | TimeUnit.Year,
UpToHour = TimeUnit.Hour | TimeUnit.Day | TimeUnit.Month | TimeUnit.Year,
UpToDay = TimeUnit.Day | TimeUnit.Month | TimeUnit.Year,
UpToMonth = TimeUnit.Month | TimeUnit.Year,
};
//
public static partial class DateTimeEx {
//
private static void _Compare( ref int result, int flags, TimeUnit tu, int a, int b ) {
var which = (int) tu;
if ( 0 != ( flags & which ) ) {
if ( a != b ) result |= which;
}
}
///<summary>Compare Dates. The returned TimeUnit will have one flag set for every different field. It will NOT indicate which date is bigger or smaller.</summary>
public static TimeUnit Compare( this DateTime a, DateTime b, TimeUnit unit ) {
int result = 0;
var flags = (int) unit;
//ompare( ref result, flags, TimeUnit.Nanosecond, a.Nano, b.Nanosecond );
_Compare( ref result, flags, TimeUnit.Millisecond, a.Millisecond, b.Millisecond );
_Compare( ref result, flags, TimeUnit.Second, a.Second, b.Second );
_Compare( ref result, flags, TimeUnit.Minute, a.Minute, b.Minute );
_Compare( ref result, flags, TimeUnit.Hour, a.Hour, b.Hour );
_Compare( ref result, flags, TimeUnit.Day, a.Day, b.Day );
_Compare( ref result, flags, TimeUnit.Month, a.Month, b.Month );
_Compare( ref result, flags, TimeUnit.Year, a.Year, b.Year );
return (TimeUnit) result;
}
}
public static class Tests {
//
private static void TestCompare() {
var test = DateTime.UtcNow;
var ts = test.ToUnixTimestamp( true );
var test2 = DateTimeEx.ToDateTime( ts, true );
var ok = 0 == DateTimeEx.Compare( test, test2, TimeUnit.UpToSecond );
Log.Assert( ok );
ts = test.ToUnixTimestamp( false );
test2 = DateTimeEx.ToDateTime( ts, false );
ok = 0 == DateTimeEx.Compare( test, test2, TimeUnit.UpToSecond );
Log.Assert( ok );
}
}