如何在没有时间的情况下获取当前日期? [英] How to get the current date without the time?
问题描述
我可以使用以下方式获取日期和时间:
I am able to get date and time using:
DateTime now = DateTime.Now;
如何使用DateTime格式本身分别获取当前日期和时间?
How can I get the current date and time separately in the DateTime format itself?
我没有在ASP.NET(C#)中使用DateTime选择器对话框。
I am not using the DateTime picker dialog box in ASP.NET (C#).
推荐答案
好吧,您可以使用今天
属性:
Well, you can get just today's date as a DateTime
using the Today
property:
DateTime today = DateTime.Today;
或更一般地,您可以使用 日期
属性。例如,如果您想要UTC日期,则可以使用:
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"));
或使用显式格式:
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.
如果您想要一个更具表现力的日期/时间API,该API可以让您与时间分开谈论日期,可能想看看我开始的 Noda Time 项目。它尚未准备好进行生产,但是我们很想听听您想要使用它做些什么...
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...
这篇关于如何在没有时间的情况下获取当前日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!