|
Date/Time Functoids |
|
Note: XSLT 1.0 has no built-in Date/Time functions, whereas XSLT 2.0/XPath 2.0 does. |
|
|
Add Days |
|
|
|
Generates: C# |
Has XSLT Equivalent: in 2.0 only |
|
|
Emitted Code: public string DateAddDays(string date, string days) { string retval = ""; double db = 0; if (IsDate(date) && IsNumeric(days, ref db)) { DateTime dt = DateTime.Parse(date); int d = (int)db; dt = dt.AddDays(d); retval = dt.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); } return retval; }
|
|
|
XSLT 1.0 Equivalent: (none) |
|
|
XSLT 2.0 Equivalent: xs:dateTime('2007-12-12') + xdt:dayTimeDuration('PxD') Where x (in dayTimeDuration()) is number of days to add e.g. 5 days would be 'P5D' |
|
|
|
|
Date |
|
|
|
Generates: C# |
Has XSLT Equivalent: in 2.0 only |
|
|
Emitted Code: public string DateCurrentDate() { DateTime dt = DateTime.Now; return dt.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); }
|
|
|
XSLT 1.0 Equivalent: (none) |
|
|
XSLT 2.0 Equivalent: fn:current-date() |
|
|
|
|
Date and Time |
|
|
|
Generates: C# |
Has XSLT Equivalent: in 2.0 only |
|
|
Emitted Code: public string DateCurrentDateTime() { DateTime dt = DateTime.Now; string curdate = dt.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); string curtime = dt.ToString("T", System.Globalization.CultureInfo.InvariantCulture); string retval = curdate + "T" + curtime; return retval; }
|
|
|
XSLT 1.0 Equivalent: (none) |
|
|
XSLT 2.0 Equivalent: fn:current-dateTime() |
|
|
|
|
Time |
|
|
|
Generates: C# |
Has XSLT Equivalent: in 2.0 only |
|
|
Emitted Code: public string DateCurrentTime() { DateTime dt = DateTime.Now; return dt.ToString("T", System.Globalization.CultureInfo.InvariantCulture); }
|
|
|
XSLT 1.0 Equivalent: (none) |
|
|
XSLT 2.0 Equivalent: fn:current-date() |
|
|
|
|
|
Common Code (this is common code used by all the date/time functoids) |
|
|
public bool IsNumeric(string val) { if (val == null) { return false; } double d = 0; return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d); } public bool IsNumeric(string val, ref double d) { if (val == null) { return false; } return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d); } public bool IsDate(string val) { bool retval = true; try { DateTime dt = Convert.ToDateTime(val, System.Globalization.CultureInfo.InvariantCulture); } catch (Exception) { retval = false; } return retval; }
|