I have to include a C# script in my XSLT file .. So as to calculate the difference between two dateTime values(and add it to some other values) .. I need to accept all the possible formats of date .. I was suppose to do it with XSLT .. but as XSLT doesn't allow to do it (implicitely) .. I found this remedy of calling a C# script .. I have to call that XSL transformation from many different C# programs .. so It would be painful (rather impossible) to write this code in all calling C# code .. :( Can you find some remedy for this
This is the XSLT code ..
<xsl:variable name="date1" select="//date1"/>
<xsl:variable name="date2" select="//date2"/>
<msxsl:script language="C#" implements-prefix="cs">
<![CDATA[
private static string[] formats = new string[]
{
"MM/dd/yyyy HH:mm:ss tt",
"MM/dd/yyyy HH:mm:ss",
"MM/dd/yyyy H:mm:ss tt",
"MM/dd/yyyy H:mm:ss",
"M/dd/yyyy HH:mm:ss tt",
"M/dd/yyyy HH:mm:ss",
"M/dd/yyyy H:mm:ss tt",
"M/dd/yyyy H:mm:ss",
"MM/d/yyyy HH:mm:ss tt",
"MM/d/yyyy HH:mm:ss",
"MM/d/yyyy H:mm:ss tt",
"MM/d/yyyy H:mm:ss",
"M/d/yyyy HH:mm:ss tt",
"M/d/yyyy HH:mm:ss",
"M/d/yyyy H:mm:ss tt",
"M/d/yyyy H:mm:ss",
};
public string datedif(string date1, string date2) {
DateTime startTime;
DateTime endTime;
DateTime ThirdDate;
string date3="12/12/2009 00:00:00";
DateTime.TryParseExact(date1, formats, new CultureInfo("en-US"),
DateTimeStyles.None, out startTime);
DateTime.TryParseExact(date2, formats, new CultureInfo("en-US"),
DateTimeStyles.None, out endTime);
DateTime.TryParseExact(date3, formats, new CultureInfo("en-US"),
DateTimeStyles.None, out ThirdDate);
ThirdDate = ThirdDate.Add(endTime.Subtract(startTime));
string result = ThirdDate.ToString("MM'/'dd'/'yyyy' 'HH':'mm':'ss");
return(result);
}
]]>
</msxsl:script>
<xsl:template match="datediff">
<xsl:element name="{local-name()}">
<xsl:value-of select="cs:datedif($date1, $date2)" />
</xsl:element>
</xsl:template>
and the errors are :
The name 'DateTimeStyles' does not exist in the current context
The type or namespace name 'CultureInfo' could not be found (are you missing a using directive or an assembly reference?)
ThanQ very much .. Jon Skeet