获取当前日期只在C# [英] Get current date only in C#
问题描述
我能够用得到的日期和时间
I am able to get date and time using
DateTime now = DateTime.Now;
我怎样才能获得当前日期和时间分别以日期时间格式本身?
How can I get the current date and time separately in datetime format itself?
我不使用的DateTimePicker对话框ASP.net(C#)。
I am not using datetimepicker dialog box in ASP.net(C#).
推荐答案
那么,你可以得到的只是今天的日期为的DateTime
使用的 今天
属性:
Well, you can get just today's date as a DateTime
using the Today
property:
今天的DateTime = DateTime.Today;
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.UtcNow.Date;
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));
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.
如果你想要一个更前pressive日期/时间API,它允许你从时间说日期分别,你可能想看看的我开始野田佳彦时间项目。这不是为生产做好准备,只是还没有,但我们很想听听你想用它做什么...
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...
这篇关于获取当前日期只在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!