c# - 将日期时间格式化为特定格式

标签 c# .net datetime

我正在尝试将DateTime转换为以下格式。

2015-06-11 07:14:03.930


我尝试过

string plannedStartTime = startTime.ToString("o");



  输出:2015-06-12T16:54:47.3206929 + 05:30




string plannedStartTime = startTime.ToString("u");



  输出:2015-06-12 16:56:57Z


没有从MSDN获取任何格式化程序

还有其他格式化程序吗?

最佳答案

您的输出没有standard date and time format。您需要在具有:作为custom date and time format的区域性(如TimeSeparator)中使用InvariantCulture说明符;

string plannedStartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss.fff", 
                                             CultureInfo.InvariantCulture);


如果CurrentCulture已经将:作为TimeSeparator,则无需在ToString方法中传递第二个参数。

关于c# - 将日期时间格式化为特定格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30755049/

相关文章:

c# - 将调用动态重定向到不同的程序集?

c# - StructureMap 创建实例

c# - 在运行时更新网格对撞机的替代方案?

c# - 在 .NET 中检查/读取/写入 cookie,请帮助我理解

.net - 将 WCF .NET 3.5 发布到 IIS 6 (Windows Server 2003)

python - 如何用python减去日期

c# - ASP.NET MVC 如何处理来自数据库的动态日期时间格式

c# - 如何在 C# 中将 DateTime 格式化为 "Oct. 10, 2008 10:43am CST"

javascript - 使用 UmbracoV7 中的选择框更改每页的项目数

C# 使用 Xamarin 导入 MySql