[英].NET convert Datetime to format Sortable date/time pattern (“s”);
Im working with VS2008, .NET and C#, and I need to send to one of our clients a DATETIME variable. 我正在使用VS2008,.NET和C#,我需要向我们的客户发送一个DATETIME变量。
The problem is that they want the Date in the format Sortable date/time pattern ("s"). 问题是他们希望Date以可排序日期/时间模式(“s”)的格式。
When I get the actual datetime, it is a Datetime object. 当我得到实际的日期时间时,它是一个Datetime对象。 When I format it to the given format is now a String object, and it has the format I want.
当我将其格式化为给定格式时,现在是一个String对象,它具有我想要的格式。 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.
但之后我无法使用相同格式的格式化String创建Datetime对象,因为它始终将其返回到原始Datetime格式。
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? 有没有办法获得具有所需格式的Datetime对象,或者Datetime对象使用给定的格式,并且您不能将它们格式化为等效的字符串格式?
Thx 谢谢
A DateTime
is just a number. DateTime
只是一个数字。 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.
因此,每当您需要将DateTime作为字符串时,您必须指定所需的格式。
String date = String.Format("{0:s}", currTime);
This can be shorted a bit to : 这可以缩短一点:
String date = currTime.ToString("s");
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 since DateTime.MinValue
or whatever it is). DateTime
对象本身不是可格式化的,它本质上只是一个数值(自DateTime.MinValue
以来的刻度数或其他任何值)。
You can convert a DateTime
object into a string
representation in whatever format you like, but you aren't changing the actual DateTime
object. 您可以将
DateTime
对象转换为您喜欢的任何格式的string
表示形式,但您不会更改实际的DateTime
对象。
Every time you use a DateTime
value in a place where it needs to be turned into a string (eg in string.Format()
), C# will generally call the .ToString()
method. 每次在需要将其转换为字符串的地方使用
DateTime
值时(例如在string.Format()
),C#通常会调用.ToString()
方法。 The DateTime
type declares a .ToString()
method that has the format you don't want. DateTime
类型声明了一个具有您不想要的格式的.ToString()
方法。
However, DateTime
has additional methods, including .ToString(IFormatProvider provider)
and .ToString(string format)
. 但是,
DateTime
还有其他方法,包括.ToString(IFormatProvider provider)
和.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: 因此,如果将相关类似字符串的上下文中的
DateTime
变量的每次使用替换为调用相应.ToString
重载的变量,则可以实现所需的功能,例如:
Instead of 代替
var message = string.Format("The parcel was sent on {0}.", currTime);
use 采用
var message = string.Format("The parcel was sent on {0}.", currTime.ToString("s"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.