仅在C#中获取当前日期 [英] Get current date only in C#

查看:93
本文介绍了仅在C#中获取当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 DateTime format itself?

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

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

推荐答案

嗯,你可以使用 DateTime microsoft.com/en-us/library/system.datetime.today.aspx> 今天 属性:

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,您可以与时间分开谈论日期,您可能想查看我开始的 Noda时间项目。它还没有准备好生产,但我们很乐意听到你想要做的...

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆