备注

 
 

format 参数应包含单个格式说明符 (请参阅 标准日期和时间格式字符串) 或自定义格式模式 (请参阅 Cadenas con formato de fecha y hora personalizado),它定义了返回字符串的格式。如果 format 是 null 或使用空字符串,常规格式说明符,G。

此方法使用派生自当前区域性的格式设置信息。有关详细信息,请参阅CurrentCulture

对调用方的说明:

ToString(String) 方法使用由当前区域性的日历中返回的字符串表示形式的日期和时间。如果当前值 DateTime 实例是早于Calendar.MinSupportedDateTime 或更高版本比 Calendar.MaxSupportedDateTime, ,该方法将引发 ArgumentOutOfRangeException。下面的示例进行了这方面的演示。它会尝试设置的范围之外的日期的格式 HebrewCalendar 类在当前区域性为希伯来语 (以色列) 时。

示例

using System;
using System.Globalization;
using System.Threading; public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(, , );
CultureInfo dft;
CultureInfo heIL = new CultureInfo("he-IL");
heIL.DateTimeFormat.Calendar = new HebrewCalendar(); // Change current culture to he-IL.
dft = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = heIL; // Display the date using the current culture's calendar.
try {
Console.WriteLine(date1.ToString("G"));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0} is earlier than {1} or later than {2}",
date1.ToString("d", CultureInfo.InvariantCulture),
heIL.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
heIL.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture));
} // Restore the default culture.
Thread.CurrentThread.CurrentCulture = dft;
}
}
// The example displays the following output:
// 07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239

下面的示例使用的每个标准日期和时间格式字符串和选定的自定义日期和时间格式字符串来显示的字符串表示形式 DateTime 值。该示例的线程当前区域性为 EN-US。

using System;

public class DateToStringExample
{
public static void Main()
{
DateTime dateValue = new DateTime(, , , , , );
// Create an array of standard format strings.
string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o",
"R", "s", "t", "T", "u", "U", "y"};
// Output date and time using each standard format string.
foreach (string standardFmt in standardFmts)
Console.WriteLine("{0}: {1}", standardFmt,
dateValue.ToString(standardFmt));
Console.WriteLine(); // Create an array of some custom format strings.
string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f",
"dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
// Output date and time using each custom format string.
foreach (string customFmt in customFmts)
Console.WriteLine("'{0}': {1}", customFmt,
dateValue.ToString(customFmt));
}
}
// This example displays the following output to the console:
// d: 6/15/2008
// D: Sunday, June 15, 2008
// f: Sunday, June 15, 2008 9:15 PM
// F: Sunday, June 15, 2008 9:15:07 PM
// g: 6/15/2008 9:15 PM
// G: 6/15/2008 9:15:07 PM
// m: June 15
// o: 2008-06-15T21:15:07.0000000
// R: Sun, 15 Jun 2008 21:15:07 GMT
// s: 2008-06-15T21:15:07
// t: 9:15 PM
// T: 9:15:07 PM
// u: 2008-06-15 21:15:07Z
// U: Monday, June 16, 2008 4:15:07 AM
// y: June, 2008
//
// 'h:mm:ss.ff t': 9:15:07.00 P
// 'd MMM yyyy': 15 Jun 2008
// 'HH:mm:ss.f': 21:15:07.0
// 'dd MMM HH:mm:ss': 15 Jun 21:15:07
// '\Mon\t\h\: M': Month: 6
// 'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00

连接:https://msdn.microsoft.com/zh-cn/library/zdtaw1bw(v=vs.110).aspx

最新文章

  1. Linux (Centos)操作MySql命令
  2. Struts2 的 值栈和ActionContext
  3. QTableWidget控件总结<二>
  4. COJ978 WZJ的数据结构(负二十二)
  5. 47.MIF和COE文件格式
  6. 解决eclipse-helios中Errors running builder JavaScript Validator的问题
  7. LwIP源代码文件目录解析
  8. [Papers]NSE, $u_3$, Lebesgue space [Kukavica-Ziane, Nonlinearity, 2006]
  9. JS中的phototype是JS中比较难理解的一个部分
  10. PostgreSQL版本快速升级
  11. Android简易实战教程--第十二话《代码获取手机总运行内存的大小》
  12. MySQL数据库 Too many connections
  13. 学习Spring Boot:(十)使用hibernate validation完成数据后端校验
  14. Linux遇到的问题(一)Ubuntu报“xxx is not in the sudoers file.This incident will be reported” 错误解决方法
  15. 2018.08.29 NOIP模拟 movie(状压dp/随机化贪心)
  16. Redis有序集内部实现原理分析
  17. BZOJ4919 [Lydsy1706月赛]大根堆 【dp + 启发式合并】
  18. 【51nod】1565 模糊搜索
  19. 封装自己的getClassName函数
  20. for和foreach的一点总结

热门文章

  1. Linq专题之提高编码效率—— 第一篇 Aggregate方法
  2. Java 流(Stream)、文件(File)和IO
  3. oracle行转列与列转行
  4. Linux下磁盘挂载
  5. 用alarmmanager 多次发送PendingIntent
  6. ZBrush中的动态网格该怎么进行运用
  7. ACM失败之路
  8. 2.一个EJB的小Demo
  9. http协议进阶(二)URL与资源
  10. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 密码强化、网络安全强化