Reputation: 13145
Does c# have support for converting two DateTime
's to the xs:duration
data type? (I'm assuming I need two DateTime
values for this?)
References: http://www.w3.org/TR/xmlschema-2/#duration and http://www.w3schools.com/schema/schema_dtypes_date.asp (half way down)
Upvotes: 2
Views: 3419
Reputation: 13145
There was support for this in the XMLConvert
class as explained here: http://kennethxu.blogspot.de/2008/09/xmlserializer-doesn-serialize-timespan.html
I ended up using this code and it displays the value in the xml correct
[XmlElementAttribute("ValidThrough", DataType = "duration")]
[DataMember(Name = "ValidThrough")]
[DefaultValue("P10D")]
public string ValidThrough
{
get
{
return XmlConvert.ToString(_validThroughField);
}
set
{
_validThroughField= XmlConvert.ToTimeSpan(value);
}
}
[XmlIgnore]
public TimeSpan _validThroughField { get; set; }
Upvotes: 5
Reputation: 223267
TimeSpan is what you are looking for.
A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, the DateTime or DateTimeOffset structure should be used instead.
Example:
DateTime dt1 = new DateTime(2012, 10, 2, 10, 20, 00);
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt1 - dt2;
Upvotes: 3