实例:
namespace Demo
{
class Program
{
static void Main(string[] args)
{
DateTime strDate = DateTime.Now;
Console.WriteLine(string.Format("短日期格式:{0:d}",strDate));
Console.WriteLine(string.Format("长日期格式:{0:D}",strDate));
Console.WriteLine(string.Format("完整日期时间格式:{0:f}",strDate));
Console.WriteLine(string.Format("短时间格式:{0:t}",strDate));
Console.WriteLine(string.Format("长时间格式:{0:T}",strDate));
Console.WriteLine(string.Format("月日格式:{0:M}",strDate));
Console.WriteLine(string.Format("年月格式:{0:Y}",strDate));
Consloe.ReadLine();
}
}
}
也可以通过ToString()的 方法来格式化日期类型数据,如:
DateTime dTime = DateTime.Now;
Console.WriteLine(dTime.ToString("Y"));