c# - 如何使用 UTC datetime 和 mongodb c# driver 比较两个 datetimeoffset
问题描述
我正在尝试使用 mongodb c# 驱动程序将 2 个 datetimeoffset 与 2 个不同的时区进行比较。我正在使用 datetimeoffset 的文档序列化来创建一个对象,该对象包括: - UTC 中的日期时间 - LOCAL 中的刻度 - 偏移量
我只想比较对象的日期时间部分,因为“2019-05-03 10:00:00 +01”等于“2019-05-03 09:00:00 +00”。
谢谢
解决方案
有很多方法可以比较DateTimeOffset
。这些方法比较 UTC 值DateTimeOffset
比较返回一个 int:
var c = DateTimeOffset.Compare(dto1, dto2);
// c > 0: dto1 is later
// c < 0: dto2 is later
// c == 0: dto1 and dto2 are the same in UTC
CompareTo类似于 compare(不同的语法),返回一个 int:
var c = dto1.CompareTo(dto2);
// c > 0: dto1 is later
// c < 0: dto2 is later
// c == 0: dto1 and dto2 are the same in UTC
等于返回布尔值:
var c = dto1.Equals(dto2);
// True: dto1 and dto2 have the same value in UTC
// False: dto1 and dto2 do not have the same UTC value
EqualsExact比较偏移量和时间,并返回一个布尔值:
var c = dto1.EqualsExact(dto2);
// True: dto1 and dto2 have the same value in UTC AND the same Offset
// False: dto1 and dto2 do not have the same UTC value or do not have the same Offset
看到这个小提琴。
推荐阅读
- c# - 相对于其局部轴将速度添加到对象
- python - 仅将两个字典与第一个字典中的键合并
- php - SQL不更新和插入数据到数据库
- c++ - 单击可检查按钮时如何避免Qt执行QAbstractButton::nextCheckState()?
- javascript - 为什么console.log 显示所有内容,并且只打印一个?
- c# - 在 C# 中保存应用程序配置的最佳方式
- java - 使用 Neo4j 进行 Spring 身份验证
- javascript - 在 TypeScript 中迭代对象的键和值
- javascript - Webpack 5 Html Webpack Plugin 错误“错误:子编译失败:”
- python - 如何在 for 循环中使用 Django 模板过滤器?