gpt4 book ai didi

c# - DateTime.Now 没有小数秒

转载 作者:数据小太阳 更新时间:2023-10-29 02:21:55 26 4
gpt4 key购买 nike

我怎样才能将 DateTime.Now 实际转换为 xsd:datetime 格式 2004-04-12T13:20:00-05:00最后将其分配给架构 XSD 中 xsd:datetime 类型的 DateTime 属性(不是 string)?

属性:

public System.DateTime Timestamp {
get {
return this.timestampField;
}
set {
this.timestampField = value;
}

XSD:

<!-- Timestamp Type - Timezone portion is required and fractional seconds are prohibited -->
<xsd:simpleType name="TimestampType">
<xsd:annotation>
<xsd:documentation>Base type for a date and time stamp</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:dateTime">
<xsd:pattern value="[1-9][0-9]{3}\-.+T[^\.]+(Z|[\+\-].+)"/>
</xsd:restriction>
</xsd:simpleType>

尝试以下:

Timestamp = Datetime.Now;
Timestamp = DateTime.ParseExact(DateTime.Now.ToString("O"), "O", CultureInfo.InvariantCulture);

我在根据 xsd 模式验证无效的 xml 中得到关注

<Timestamp>2014-07-18T17:27:04.3185791-04:00</Timestamp>

我正在尝试找出如何为根据 xsd 模式有效的 Timestamp 属性分配适当的值。即,2004-04-12T13:20:00-05:00 没有小数秒。

最佳答案

如果你看这个MSDN article你发现它说(强调我的):

Internally, DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight, January 1, 0001. The actual DateTime value is independent of the way in which that value appears when displayed in a user interface element or when written to a file. The appearance of a DateTime value is the result of a formatting operation. Formatting is the process of converting a value to its string representation.

Because the appearance of date and time values is dependent on such factors as culture, international standards, application requirements, and personal preference, the DateTime structure offers a great deal of flexibility in formatting date and time values through the overloads of its ToString method. The default DateTime.ToString() method returns the string representation of a date and time value using the current culture's short date and long time pattern. The following example uses the default DateTime.ToString() method to display the date and time using the short date and long time pattern for the en-US culture, the current culture on the computer on which the example was run.

因此,基于此,您可能想尝试从您从 DateTime.Now 创建的 TimeStamp 的特定字段创建一个新的 DateTime 对象,这将清零表示时间间隔小于一秒的滴答声。如果这不起作用,您可能使用了错误的类型,请改用字符串。

Timestamp = Datetime.Now;
Timestamp = new DateTime(Timestamp.Year,Timestamp.Month,Timestamp.Day,Timestamp.Hour,Timestamp.Minute,Timestamp.Second,Timestamp.Kind);

关于c# - DateTime.Now 没有小数秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24835700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com