How to format DATE in XSLT 1.0

故事扮演 提交于 2019-12-12 17:42:12

问题


I searched a bit but couldn't find the answer.

I want to get current date and format to YYYYMMDD

I cannot use EXSLT as per my requirements.


回答1:


A very simple Inline C# Script Functoid could look like this:

public string MyDateFormat(string dateValue)
{
    string result = String.Empty;
    string outputFormat = "{0:yyyyMMdd}";

    DateTime parsed;

    if (DateTime.TryParse(dateValue, out parsed))
    {
        result = String.Format(outputFormat, parsed);
    }
    else
    {
        result = String.Format(outputFormat, DateTime.MinValue);
    }

    return result;
}

For a similar problem I created a External Assembly which will allow to specify CultureInfo for parsing the input DateTime string and also submit the output format string as a functoid input parameter.




回答2:


You want the substring to act on the date.

To get the date:

substring-before($dateTime, 'T')

To get the year you want to work on the above result:

substring-before($previousResult, '-')

Then concatenate the values you got from the string manipulations.

This explains the whole thing and wraps it on a template: Format a date in XML via XSLT

etc

Hope this helps.



来源:https://stackoverflow.com/questions/5309370/how-to-format-date-in-xslt-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!