C# 中的 DateTime.ToString() 方法
C# 中的 DateTime.ToString() 方法用于将当前 DateTime 对象的值转换为其等效的字符串表示形式。
语法
以下是语法 −
ToString(String, IFormatProvider) ToString() ToString(IFormatProvider) ToString(String)
示例
现在让我们看一个示例来实现 DateTime.ToString() 方法 −
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 11, 11, 7, 11, 25);
Console.WriteLine("Date = {0}", d);
string str = d.ToString();
Console.WriteLine("String representation = {0}", str);
}
}输出
这将产生以下输出 −
Date = 11/11/2019 7:11:25 AM String representation = 11/11/2019 7:11:25 AM
示例
现在让我们看另一个示例来实现 DateTime.ToString(String) 方法 −
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 11, 11, 7, 11, 25);
Console.WriteLine("Date = {0}", d);
string str = d.ToString("f");
Console.WriteLine("String representation = {0}", str);
}
}输出
这将产生以下输出 −
Date = 11/11/2019 7:11:25 AM String representation = Monday, November 11, 2019 7:11 AM
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP