.NET 将日期时间转换为可排序日期/时间模式(“s”)的格式;
我正在使用 VS2008、.NET 和 C#,我需要向我们的一个客户发送一个 DATETIME 变量。
问题是他们希望日期采用可排序日期/时间模式(“s”)格式。
当我获得实际的日期时间时,它是一个日期时间对象。当我将其格式化为给定格式时,现在它是一个 String 对象,并且它具有我想要的格式。但之后我无法从具有相同格式的格式化字符串创建日期时间对象,因为它总是将其返回到原始日期时间格式。
更具体地说:
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.")
是否有一种方法可以获取具有所需格式的 Datetime 对象,或者 Datetime 对象使用给定的格式,而您无法将它们格式化为等效的字符串格式?
谢谢
Im working with VS2008, .NET and C#, and I need to send to one of our clients a DATETIME variable.
The problem is that they want the Date in the format Sortable date/time pattern ("s").
When I get the actual datetime, it is a Datetime object. When I format it to the given format is now a String object, and it has the format I want. But after that I can't create a Datetime object from that formatted String with the same format, because it always returns it to the original Datetime format.
More specific:
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.")
Is there a way of getting a Datetime object with the desired format, or Datetime objects use a given format, and you can't format them to the equivalent string formats?
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DateTime
只是一个数字。它没有内在的“格式”。它仅在转换为字符串时才呈现为格式。因此,每当您需要 DateTime 作为字符串时,您都必须指定您想要的格式。这可以稍微缩短为:
A
DateTime
is just a number. It has no intrinsic "format". It is only rendered into a format when converted to a string. Hence, whenever you need a DateTime as a string, you have to specify what format you want it in.This can be shorted a bit to :
如果我正确理解这个问题,我认为你感到困惑。
DateTime
对象本身不可格式化,它本质上只是一个数值(自DateTime.MinValue
或其他任何值以来的刻度数)。您可以将
DateTime
对象转换为您喜欢的任何格式的string
表示形式,但您不会更改实际的DateTime< /代码> 对象。
If I understand the question correctly, I think you are getting confused. A
DateTime
object itself is not formattable, it is essentialy just a numeric value (number of ticks sinceDateTime.MinValue
or whatever it is).You can convert a
DateTime
object into astring
representation in whatever format you like, but you aren't changing the actualDateTime
object.每次在需要将
DateTime
值转换为字符串的地方(例如在string.Format()
中)使用时,C# 通常会调用.ToString()
方法。DateTime
类型声明了一个.ToString()
方法,该方法具有您不想要的格式。但是,
DateTime
还有其他方法,包括.ToString(IFormatProviderprovider)
和.ToString(string format)
。因此,如果您将相关字符串类上下文中对
DateTime
变量的每次使用替换为调用适当的.ToString
重载的变量,您可能可以实现您想要的目标,例如示例:代替
使用
Every time you use a
DateTime
value in a place where it needs to be turned into a string (e.g. instring.Format()
), C# will generally call the.ToString()
method. TheDateTime
type declares a.ToString()
method that has the format you don’t want.However,
DateTime
has additional methods, including.ToString(IFormatProvider provider)
and.ToString(string format)
.Therefore, you can probably achieve what you want if you replace every use of a
DateTime
variable in the relevant string-like context to one that calls the appropriate.ToString
overload, for example:Instead of
use