C# 如何将时间设置为当天的午夜?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13467230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:41:03  来源:igfitidea点击:

How to set time to midnight for current day?

c#.net

提问by genxgeek

Every time that I create a non-nullable datetime in my mvc3 application it defaults to now(), where now is current date with current time. I would like to default it to today's date with 12am as the time.

每次我在我的 mvc3 应用程序中创建一个不可为空的日期时间时,它默认为 now(),其中 now 是当前日期和当前时间。我想将其默认为今天的日期,时间为上午 12 点。

I'm trying to default the time in my mvc...but...the following isn't setting to todays date @12am. Instead it defaults to now with current date and time.

我正在尝试在我的 mvc 中默认时间......但是......以下没有设置为今天的日期@12am。相反,它默认为当前日期和时间。

private DateTime _Begin = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 12, 0, 0);
public DateTime Begin { get { return _Begin; } set { _Begin = value; } } 

How can I set to 12am for the current date for non-nullable datetime?

如何将不可为空的日期时间的当前日期设置为上午 12 点?

采纳答案by Chris Moutray

You can use the Dateproperty of the DateTime object - eg

您可以使用DateDateTime 对象的属性 - 例如

DateTime midnight = DateTime.Now.Date;

So your code example becomes

所以你的代码示例变成

private DateTime _Begin = DateTime.Now.Date;
public DateTime Begin { get { return _Begin; } set { _Begin = value; } }

PS. going back to your original code setting the hours to 12will give you time of noonfor the current day, so instead you could have used 0...

附注。回到您的原始代码,将小时数设置为12将为您提供当天的中午时间,因此您可以使用0...

var now = DateTime.Now;
new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);

回答by Justin Pihony

I believe you are looking for DateTime.Today. The documentation states:

我相信你正在寻找DateTime.Today。该文件指出:

An object that is set to today's date, with the time component set to 00:00:00.

设置为今天日期的对象,时间部分设置为 00:00:00。

http://msdn.microsoft.com/en-us/library/system.datetime.today.aspx

http://msdn.microsoft.com/en-us/library/system.datetime.today.aspx

Your code would be

你的代码是

DateTime _Begin = DateTime.Today;

回答by Abhishek Dheeman

Try this:

尝试这个:

DateTime Date = DateTime.Now.AddHours(-DateTime.Now.Hour).AddMinutes(-DateTime.Now.Minute)
   .AddSeconds(-DateTime.Now.Second);

Output will be like:

输出将类似于:

07/29/2015 00:00:00

07/29/2015 00:00:00

回答by Ravi Ram

Using some of the above recommendations, the following function and code is working for search a date range:

使用上述一些建议,以下函数和代码可用于搜索日期范围:

Set date with the time component set to 00:00:00

将日期设置为 00:00:00

public static DateTime GetDateZeroTime(DateTime date)
{
    return new DateTime(date.Year, date.Month, date.Day, 0, 0, 0);
}

Usage

用法

var modifieddatebegin = Tools.Utilities.GetDateZeroTime(form.modifieddatebegin);

var modifieddateend = Tools.Utilities.GetDateZeroTime(form.modifieddateend.AddDays(1));

回答by Reno

Only need to set it to

只需要设置为

DateTime.Now.Date

DateTime.Now.Date

Console.WriteLine(DateTime.Now.Date.ToString("yyyy-MM-dd HH:mm:ss"));
Console.Read();

It shows

表明

"2017-04-08 00:00:00"

“2017-04-08 00:00:00”

on my machine.

在我的机器上。

回答by Rusty West

Most of the suggested solutions can cause a 1 day error depending on the time associated with each date. If you are looking for an integer number of calendar days between to dates, regardless of the time associated with each date, I have found that this works well:

大多数建议的解决方案可能会导致 1 天的错误,具体取决于与每个日期相关的时间。如果您正在寻找日期之间的整数个日历天数,无论与每个日期相关联的时间如何,我发现这很有效:

return (dateOne.Value.Date - dateTwo.Value.Date).Days;

回答by Rob L

Related, so I thought I would post for others. If you want to find the UTC of the start of today (for your timezone) the following code works for any UTC offset (-23.5 thru +23.5). This looks like we add X hours then subtract X hours, but the important thing is the ".Date" after the add.

相关,所以我想我会为其他人发帖。如果您想找到今天开始的 UTC(对于您的时区),以下代码适用于任何 UTC 偏移量(-23.5 到 +23.5)。看起来我们添加 X 小时然后减去 X 小时,但重要的是添加后的“.Date”。

double utcOffset= 10.0;  // Set to your UTC offset in hours (eg. Melbourne Australia)
var now = DateTime.UtcNow;

var startOfToday = now.AddHours(utcOffset - 24.0).Date;
startOfToday = startOfToday.AddHours(24.0 - utcOffset);