gpt4 book ai didi

c# - 如何转换日期时间?到日期时间

转载 作者:IT王子 更新时间:2023-10-29 03:31:18 25 4
gpt4 key购买 nike

我想将可为 null 的 DateTime (DateTime?) 转换为 DateTime,但出现错误:

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

我尝试了以下操作:

DateTime UpdatedTime = (DateTime)_objHotelPackageOrder.UpdatedDate == null 
? DateTime.Now : _objHotelPackageOrder.UpdatedDate;

最佳答案

您想使用 null-coalescing operator ,正是为此目的而设计的。

使用它你最终得到这段代码。

DateTime UpdatedTime = _objHotelPackageOrder.UpdatedDate ?? DateTime.Now;

关于c# - 如何转换日期时间?到日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1091870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com