比较 DateTime 对象

发布于 2024-11-18 19:44:19 字数 356 浏览 2 评论 0原文

目标 - 找出哪个日期时间更新。

我可以用这段代码解决这个问题:

DateTime dt1 = new DateTime(...); //let's say it was created on 1/1/2000

DateTime dt2 = new DateTime(...); //let's say it was create on 1/1/2011 

if (dt2.ToBinary() > dt1.ToBinary()) {
print dt2 is newer than dt1 }

我可以简单地将 DateTime 对象转换为二进制,然后假设较大的对象更新吗?

谢谢, 凯文

Goal - find out which DateTime is more recent.

Can I figure this out with this code:

DateTime dt1 = new DateTime(...); //let's say it was created on 1/1/2000

DateTime dt2 = new DateTime(...); //let's say it was create on 1/1/2011 

if (dt2.ToBinary() > dt1.ToBinary()) {
print dt2 is newer than dt1 }

Can I simply convert the DateTime objects to binary, then presume that the larger one is more recent?

Thanks,
Kevin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

对你而言 2024-11-25 19:44:19
if (dt2 > dt1) {
print dt2 is newer than dt1 }

应该足够了,因为 DateTime 会重载比较运算符。

if (dt2 > dt1) {
print dt2 is newer than dt1 }

should be enough as DateTime overloads the comparison operators.

双手揣兜 2024-11-25 19:44:19

您通常可以做得更好:

if (dt2 > dt1)

棘手的一点是考虑时区...您可以使用,

if (dt2.ToUniversalTime() > dt1.ToUniversalTime())

但前提是您知道任何“本地”时间确实是系统时区的本地时间。

.NET 中的日期和时间有点混乱:(

You can usually do better than that:

if (dt2 > dt1)

The tricky bit is taking time zones into consideration... you can potentially use

if (dt2.ToUniversalTime() > dt1.ToUniversalTime())

but only if you know that any "local" times really are local in the system's time zone.

Dates and times in .NET are a bit of a mess :(

萝莉病 2024-11-25 19:44:19

不,你不能。 ToBinary() 返回还存储时区信息的内部格式。

相反,您可以直接比较 DateTime

if (dt2 > dt1)

DateTime 会重载比较运算符。

您可以比较 Ticks 属性,但您不必费心。

No, you can't. ToBinary() returns an internal format that also stores time zone info.

Instead, you can compare the DateTimes directly:

if (dt2 > dt1)

DateTime overloads the comparison operators.

You could compare the Ticks property, but you shouldn't bother.

优雅的叶子 2024-11-25 19:44:19

您可以使用 Datetime.Compare

int iDiff = DateTime.Compare(new DateTime(2011, 02, 28), new DateTime(2011, 01, 30));

-1 = 第一个日期小于第二个日期
0 = 第一个日期等于第二个日期
1 = 第一个日期大于第二个日期

You can use Datetime.Compare

int iDiff = DateTime.Compare(new DateTime(2011, 02, 28), new DateTime(2011, 01, 30));

-1 = The Fisrt Date is Less than the Second
0 = The First Date is equal than the Second
1 = The First Date is Greater than the Second

写下不归期 2024-11-25 19:44:19

DateTime 对象本身是可比较的,因此

if (dt1>dt2)
  Console.WriteLine('dt1 is newer');
else if (dt1>dt2)
  Console.WriteLine('dt2 is newer');
else // they are equal
  Console.WriteLine('dt1 and dt2 are the same');

也可以工作。
此外 DateTime 实现了 IComparable 接口,因此您可以执行以下操作:

int result = dt1.CompareTo(dt2);
if (result > 0)
  Console.WriteLine('dt1 is newer');
else if (result < 0)
  Console.WriteLine('dt2 is newer');
else // result = 0
  Console.WriteLine('dt1 and dt2 are the same');

编辑:这会忽略时区、当地时间等...

DateTime objects are comparable themselves, so

if (dt1>dt2)
  Console.WriteLine('dt1 is newer');
else if (dt1>dt2)
  Console.WriteLine('dt2 is newer');
else // they are equal
  Console.WriteLine('dt1 and dt2 are the same');

will work too.
In addition DateTime implements the IComparable<DateTime> interface, so you can do:

int result = dt1.CompareTo(dt2);
if (result > 0)
  Console.WriteLine('dt1 is newer');
else if (result < 0)
  Console.WriteLine('dt2 is newer');
else // result = 0
  Console.WriteLine('dt1 and dt2 are the same');

EDIT: This ignores time zones, local times, etc...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文