C# 中 DateTime 转换为 Date,然后返回 DateTime
我用它来将 DateTime 值转换为 Date,然后添加 00:00:00 和 23:59:59 以确保在计数时考虑到一整天。我很确定这是错误的做事方式。什么是正确的方法?
DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00");
DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59");
I use this to convert DateTime value into Date and then I add 00:00:00 and 23:59:59 to make sure whole day is taken into consideration when counting stuff. I'm pretty sure it's wrong way of doing things. What would be the right way?
DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00");
DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
日期时间 newDate = 新日期时间( oldDate.Year, oldDate.Month, oldDate.Day, 23, 59,59 )
日期时间 newDate = 新日期时间( oldDate.Year, oldDate.Month, oldDate.Day, 0, 0, 0 )
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 23, 59,59 )
DateTime newDate = new DateTime( oldDate.Year, oldDate.Month, oldDate.Day, 0, 0, 0 )
您可以使用 TimeSpan:
这样您至少可以避免解析,这可能会失败,具体取决于本地文化设置。
You could work with TimeSpan:
Like that you avoid at least the parsing, which can fail depending on the local culture settings.
如果
dateTimeWycenaPortfelaObliczDataOd
的类型为DateTime
,您可以使用:仅获取日期部分(时间将为 00:00:00...)。
如果您想获取日期的最后一个刻度,您可以使用:
但您最好使用下一个日期(.AddDays(1))。
在任何情况下,都不需要转换为字符串并返回日期时间。
if
dateTimeWycenaPortfelaObliczDataOd
is of typeDateTime
, You can use:to get the date part only (time will be 00:00:00...).
If you want to get the very last tick of the date, you can use:
but you really better work with the next date (.AddDays(1)).
In any case, there is no need to convert to string and back to DateTime.
DateTime 对象有一个 Date 属性,这可能是什么你需要。
DateTime objects have a Date property which might be what you need.
您可以在 DateTime 对象上使用以下属性/方法来获取您的值:
You can use the following properties / methods on a DateTime object to get your values :
了解为什么需要它会有所帮助,但这会起作用。
使用
Date
属性,然后直接操作它们来创建所需的时间组件 - 无需费心解析和转换。It would help to know why you're needing it, but this would work.
Using the
Date
attribute and then manipulating them directly to create the required time component - no need to bother with parsing and conversion.您可以使用 DateTime 对象的 Date 属性来完成您的需要。
如果您确实希望它在 23:59:59 结束,您可以执行以下操作:
将 varObliczDo 设置为您的结束日期,没有时间加一天(午夜)。因此,如果
dateTimeWycenaPortfelaObliczDataDo
是2010-03-05 16:12:12
,那么现在将是2010-03-06 00:00:00
。You could use the Date property of the DateTime object to accomplish what you need.
If you really want it to end at 23:59:59 you can do:
Will set varObliczDo to be your ending date with no time plus one day (at midnight). So if
dateTimeWycenaPortfelaObliczDataDo
was2010-03-05 16:12:12
it would now be2010-03-06 00:00:00
.也许是这样的?这是我凭空写出来的,代码中可能有一些错误。
Something like this maybe? I've typed this out of my head, there are probably some mistakes in the code.