小编And*_*lez的帖子

.NET将Datetime转换为格式可排序的日期/时间模式("s");

我正在使用VS2008,.NET和C#,我需要向我们的客户发送一个DATETIME变量.

问题是他们希望Date以可排序日期/时间模式("s")的格式.

当我得到实际的日期时间时,它是一个Datetime对象.当我将其格式化为给定格式时,现在是一个String对象,它具有我想要的格式.但之后我无法使用相同格式的格式化String创建Datetime对象,因为它始终将其返回到原始Datetime格式.

更加具体:

DateTime currTime = System.DateTime.Now; //(the format is "13/08/2010 09:33:57 a.m.")

String date = String.Format("{0:s}", currTime);// (wanted format "2010-08-13T09:33:57")

DateTime newDate = DateTime.Parse(date);// (original format again "13/08/2010 09:33:57 a.m.")

IFormatProvider culture = new System.Globalization.CultureInfo("", true); //(Invariant Culture)
String format = "s";                        
DateTime fecha = DateTime.ParseExact(date, format, culture); // (original format again "13/08/2010 09:33:57 a.m.")
Run Code Online (Sandbox Code Playgroud)

有没有办法获得具有所需格式的Datetime对象,或者Datetime对象使用给定的格式,并且您不能将它们格式化为等效的字符串格式?

谢谢

.net c# format datetime

3
推荐指数
2
解决办法
8923
查看次数

标签 统计

.net ×1

c# ×1

datetime ×1

format ×1