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
热门推荐
10 元旦祝福语20秒简短
11 侄儿婚后祝福语简短精辟
12 歌曲虎年祝福语大全简短
13 邀请函简短祝福语
14 朋友女儿生日祝福语 简短
15 妹妹定婚姐姐祝福语简短
16 婚庆公司早安祝福语简短
17 十一周岁祝福语简短
18 生日祝福语长辈大妈简短