C# 中 DateTime 转换为 Date,然后返回 DateTime

发布于 2024-08-24 04:35:51 字数 366 浏览 9 评论 0原文

我用它来将 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 技术交流群。

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

发布评论

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

评论(8

撧情箌佬 2024-08-31 04:35:52

日期时间 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 )

权谋诡计 2024-08-31 04:35:52

您可以使用 TimeSpan:

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd - new TimeSpan(dateTimeWycenaPortfelaObliczDataOd.Hours, dateTimeWycenaPortfelaObliczDataOd.Minutes, dateTimeWycenaPortfelaObliczDataOd.Seconds);

这样您至少可以避免解析,这可能会失败,具体取决于本地文化设置。

You could work with TimeSpan:

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd - new TimeSpan(dateTimeWycenaPortfelaObliczDataOd.Hours, dateTimeWycenaPortfelaObliczDataOd.Minutes, dateTimeWycenaPortfelaObliczDataOd.Seconds);

Like that you avoid at least the parsing, which can fail depending on the local culture settings.

平定天下 2024-08-31 04:35:51

如果 dateTimeWycenaPortfelaObliczDataOd 的类型为 DateTime,您可以使用:

dateTimeWycenaPortfelaObliczDataOd.Date

仅获取日期部分(时间将为 00:00:00...)。
如果您想获取日期的最后一个刻度,您可以使用:

dateTimeWycenaPortfelaObliczDataOd.Date.AddDays(1).AddTicks(-1)

但您最好使用下一个日期(.AddDays(1))。

在任何情况下,都不需要转换为字符串并返回日期时间。

if dateTimeWycenaPortfelaObliczDataOd is of type DateTime, You can use:

dateTimeWycenaPortfelaObliczDataOd.Date

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:

dateTimeWycenaPortfelaObliczDataOd.Date.AddDays(1).AddTicks(-1)

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.

凡尘雨 2024-08-31 04:35:51

DateTime 对象有一个 Date 属性,这可能是什么你需要。

DateTime objects have a Date property which might be what you need.

春夜浅 2024-08-31 04:35:51

您可以在 DateTime 对象上使用以下属性/方法来获取您的值:

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataOd.AddDayes(1).AddTicks(-1);

You can use the following properties / methods on a DateTime object to get your values :

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataOd.AddDayes(1).AddTicks(-1);
踏月而来 2024-08-31 04:35:51

了解为什么需要它会有所帮助,但这会起作用。

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = varObliczOd.AddDays(1).AddSeconds(-1);

使用 Date 属性,然后直接操作它们来创建所需的时间组件 - 无需费心解析和转换。

It would help to know why you're needing it, but this would work.

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date;
DateTime varObliczDo = varObliczOd.AddDays(1).AddSeconds(-1);

Using the Date attribute and then manipulating them directly to create the required time component - no need to bother with parsing and conversion.

本王不退位尔等都是臣 2024-08-31 04:35:51

您可以使用 DateTime 对象的 Date 属性来完成您的需要。

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Value.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1);

如果您确实希望它在 23:59:59 结束,您可以执行以下操作:

DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1).AddSeconds(-1);

将 varObliczDo 设置为您的结束日期,没有时间加一天(午夜)。因此,如果 dateTimeWycenaPortfelaObliczDataDo2010-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.

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Value.Date;
DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1);

If you really want it to end at 23:59:59 you can do:

DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1).AddSeconds(-1);

Will set varObliczDo to be your ending date with no time plus one day (at midnight). So if dateTimeWycenaPortfelaObliczDataDo was 2010-03-05 16:12:12 it would now be 2010-03-06 00:00:00.

夏至、离别 2024-08-31 04:35:51

也许是这样的?这是我凭空写出来的,代码中可能有一些错误。

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.AddSeconds(-dateTimeWycenaPortfelaObliczDataOd.Seconds).AddMinutes(-dateTimeWycenaPortfelaObliczDataOd.Minutes).AddHours(-dateTimeWycenaPortfelaObliczDataOd.Hours);

DateTime varObliczDo  = new DateTime(dateTimeWycenaPortfelaObliczDataDo.Year, dateTimeWycenaPortfelaObliczDataDo.Month, dateTimeWycenaPortfelaObliczDataDoDay, 23, 59, 59);

Something like this maybe? I've typed this out of my head, there are probably some mistakes in the code.

DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.AddSeconds(-dateTimeWycenaPortfelaObliczDataOd.Seconds).AddMinutes(-dateTimeWycenaPortfelaObliczDataOd.Minutes).AddHours(-dateTimeWycenaPortfelaObliczDataOd.Hours);

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