简体   繁体   English

如何在没有时间的情况下获取当前日期?

[英]How to get the current date without the time?

I am able to get date and time using:我可以使用以下方法获取日期和时间:

DateTime now = DateTime.Now;

How can I get the current date and time separately in the DateTime format itself?如何以 DateTime 格式本身单独获取当前日期和时间?

I am not using the DateTime picker dialog box in ASP.NET (C#).我没有使用 ASP.NET (C#) 中的日期时间选择器对话框。

Well, you can get just today's date as a DateTime using the Today property:好吧,您可以使用Today属性将Today的日期作为DateTime获取:

 DateTime today = DateTime.Today;

or more generally, you can use the Date property.或者更一般地说,您可以使用Date属性。 For example, if you wanted the UTC date you could use:例如,如果您想要 UTC 日期,您可以使用:

 DateTime dateTime = DateTime.UtcNow.Date;

It's not very clear whether that's what you need or not though... if you're just looking to print the date, you can use:不过,目前还不清楚这是否是您需要的……如果您只是想打印日期,则可以使用:

Console.WriteLine(dateTime.ToString("d"));

or use an explicit format:或使用显式格式:

Console.WriteLine(dateTime.ToString("dd/MM/yyyy"));

See more about standard and custom date/time format strings.查看有关标准自定义日期/时间格式字符串的更多信息。 Depending on your situation you may also want to specify the culture.根据您的情况,您可能还想指定文化。

If you want a more expressive date/time API which allows you to talk about dates separately from times, you might want to look at the Noda Time project which I started.如果您想要一个更具表现力的日期/时间 API,它允许您将日期与时间分开讨论,您可能需要查看我开始的Noda Time项目。 It's not ready for production just yet, but we'd love to hear what you'd like to do with it...它还没有准备好投入生产,但我们很想听听你想用它做什么......

See, here you can get only date by passing a format string.看,在这里您只能通过传递格式字符串来获取日期。 You can get a different date format as per your requirement as given below for current date:您可以根据您的要求获得不同的日期格式,如下所示:

DateTime.Now.ToString("M/d/yyyy");

Result : "9/1/2015"结果: “9/1/2015”

DateTime.Now.ToString("M-d-yyyy");

Result : "9-1-2015"结果: “9-1-2015”

DateTime.Now.ToString("yyyy-MM-dd");

Result : "2015-09-01"结果: “2015-09-01”

DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

Result : "2015-09-01 09:20:10"结果: “2015-09-01 09:20:10”

For more details take a look at MSDN reference for Custom Date and Time Format Strings有关更多详细信息,请查看自定义日期和时间格式字符串的 MSDN 参考

Use DateTime.Today property.使用DateTime.Today属性。 It will return date component of DateTime.Now .它将返回DateTime.Now日期组件。 It is equivalent of DateTime.Now.Date .它相当于DateTime.Now.Date

Current Time :当前时间 :

DateTime.Now.ToString("HH:mm:ss");

Current Date :当前的日期 :

DateTime.Today.ToString("dd-MM-yyyy");

You can use following code to get the date and time separately.您可以使用以下代码分别获取日期和时间。

DateTime now = DateTime.Now;
string date = now.GetDateTimeFormats('d')[0];
string time = now.GetDateTimeFormats('t')[0];

You can also, check the MSDN for more information.您也可以查看MSDN以获取更多信息。

There is no built-in date-only type in .NET. .NET 中没有内置的仅日期类型。

The convention is to use a DateTime with the time portion set to midnight.约定是使用时间部分设置为午夜的DateTime

The static DateTime.Today property will give you today's date.静态DateTime.Today属性将为您提供今天的日期。

As of.Net 6 you can use:从 .Net 6 开始,您可以使用:

 var dateOnly = DateOnly.FromDateTime(DateTime.Now);

Ref: https://learn.microsoft.com/en-us/do.net/api/system.dateonly?view.net-6.0参考: https://learn.microsoft.com/en-us/do.net/api/system.dateonly?view.net-6.0

采用

txtdate.Text = DateTime.Today.ToString("dd-MM-yyyy");
string now = Convert.ToString(DateTime.Now.ToShortDateString());
Console.WriteLine(now);
Console.ReadLine();

for month一个月

DateTime.Now.ToString("MM");

for day一天

DateTime.Now.ToString("dd");

for year一年

DateTime.Now.ToString("yyyy");
DateTime.Now.ToString("dd/MM/yyyy");

I think you need separately date parts like (day, Month, Year)我认为您需要单独的日期部分,例如(日,月,年)

DateTime today = DateTime.Today;今天的日期时间 = DateTime.Today;

Will not work for your case.不适用于您的情况。 You can get date separately so you don't need variable today to be as a DateTime Type, so lets just give today variable int Type because the day is only int.您可以单独获取日期,因此您不需要today变量作为DateTime类型,所以让我们只给today变量int类型,因为这一天只是 int。 So today is 10 March 2020 then the result of所以今天是 2020 年 3 月 10 日,那么结果是

int today = DateTime.Today.Day; int today = DateTime.Today.Day;

int month = DateTime.Today.Month; int 月份 = DateTime.Today.Month;

int year = DateTime.Today.Year; int year = DateTime.Today.Year;

MessageBox.Show(today.ToString()+ " - this is day. "+month.ToString()+ " - this is month. " + year.ToString() + " - this is year"); MessageBox.Show(today.ToString()+ " - 这是日。"+month.ToString()+ " - 这是月。" + year.ToString() + " - 这是年");

would be " 10 - this is day. 3 - this is month. 2020 - this is year"将是“ 10 - 这是天。3 - 这是月。2020 - 这是年”

您可以像这样使用DateTime.Now.ToShortDateString()

var test = $"<b>Date of this report:</b> {DateTime.Now.ToShortDateString()}";

.Net6 ,您可以使用新的DateOnly类型:

DateOnly.FromDateTime(DateTime.Today)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM