仅在C#中获取当前日期(How to get the current date without the time?)

编程入门 行业动态 更新时间:2024-10-11 03:17:11
仅在C#中获取当前日期(How to get the current date without the time?)

我可以使用以下日期和时间:

DateTime now = DateTime.Now;

如何以DateTime格式本身分别获取当前的日期和时间?

我不在ASP.net( C# )中使用DateTime选择器对话框。

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?

I am not using the DateTime picker dialog box in ASP.NET (C#).

最满意答案

那么您可以使用Today属性获取当天的日期DateTime :

DateTime today = DateTime.Today;

或者更一般地,您可以使用Date属性。 例如,如果您希望UTC时间可以使用:

DateTime dateTime = DateTime.UtcNow.Date;

这不是很清楚,无论是需要还是不需要,但如果你只是想打印日期,你可以使用:

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

或使用显式格式:

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

详细了解标准和自定义日期/时间格式字符串。 根据您的情况,您可能还需要指定文化。

如果你想要一个更具表现力的日期/时间API,它可以让你分享日期的时间,你可能想看看我开始的Noda Time项目。 它还没有准备好生产,但我们很乐意听到你想要做的事情...

Well, you can get just today's date as a DateTime using the Today property:

DateTime today = DateTime.Today;

or more generally, you can use the Date property. For example, if you wanted the UTC date you could use:

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. It's not ready for production just yet, but we'd love to hear what you'd like to do with it...

更多推荐

本文发布于:2023-08-07 17:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   current   time   date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!