如何给格式DateTime.Date? [英] how to give format DateTime.Date?

查看:81
本文介绍了如何给格式DateTime.Date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTime的DT = DateTime.Now
dt.Date创建于2012年10月31日创建00:00:00。它以DD.MM.YYYY格式,但我需要DD / MM / YYYY。我可以使用:返回新的datetime(d.Year,d.Month,d.Day,0,0,0);它会创建我的DD / MM / YYYY解决方案吗?请不要翻译String.i需要的datetime ...

DateTime dt = DateTime.Now dt.Date is created to 31.10.2012 00:00:00 .it is created to dd.mm.yyyy format but i need dd/mm/yyyy. Can i use: return new DateTime(d.Year, d.Month, d.Day, 0, 0, 0); it will create to me dd/mm/yyyy solution?Please dont translate String.i need datetime...

推荐答案

的DateTime 结构内部并不存储任何格式信息。如果你要输出的的DateTime 实例作为格式化字符串,你只需要调用的 的ToString() 用正确的格式字符串:

The DateTime struct doesn't store any formatting information internally. If you want to output the DateTime instance as a formatted string, you just need to call ToString() with the proper format string:

var date = DateTime.Now;
var formattedString = date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);

如果您需要到底是哪说明符在格式字符串使用的更多信息,请上网:

If you need more information on exactly which specifiers to use in your format string, check out:

MSDN - 自定义日期和时间格式字符串

这篇关于如何给格式DateTime.Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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