新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

c#中datetime转换为string怎么实现,c#转换时间

发布时间:2024-04-01 19:19:01

c#中datetime转换为string怎样实现

在C#中,将DateTime对象转换为字符串可使用DateTime对象的ToString方法。以下是将DateTime对象转换为字符串的示例代码:

DateTime dateTime = DateTime.Now;
string dateString = dateTime.ToString(); // 默许格式化为 "yyyy-MM-dd HH:mm:ss"
Console.WriteLine(dateString);

如果想要自定义日期时间的格式,可使用ToString方法的重载版本,传入格式化字符串作为参数,例如:

string customDateString = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(customDateString);

可以根据需要选择适合的格式化字符串来转换DateTime对象为指定格式的字符串。